Skip to content

Commit

Permalink
Revert "feat: add a warning for invalid arguments with suspense mode (v…
Browse files Browse the repository at this point in the history
…ercel#1402)"

This reverts commit c703e6c.
  • Loading branch information
koba04 committed Sep 14, 2021
1 parent 29eb89f commit 5e579e0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 52 deletions.
4 changes: 0 additions & 4 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ export const useSWRHandler = <Data = any, Error = any>(
const data = isUndefined(cached) ? fallback : cached
const error = cache.get(keyErr)

if (suspense && (!key || !fn)) {
throw new Error('useSWR requires either key or fetcher with suspense mode')
}

// A revalidation must be triggered when mounted if:
// - `revalidateOnMount` is explicitly set to `true`.
// - `isPaused()` returns `false`, and:
Expand Down
53 changes: 5 additions & 48 deletions test/use-swr-suspense.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import React, { ReactNode, Suspense, useEffect, useState } from 'react'
import useSWR, { mutate } from 'swr'
import { createResponse, sleep } from './utils'

class ErrorBoundary extends React.Component<{ fallback?: ReactNode }> {
state = { hasError: false, message: null }
static getDerivedStateFromError(error: Error) {
class ErrorBoundary extends React.Component<{ fallback: ReactNode }> {
state = { hasError: false }
static getDerivedStateFromError() {
return {
hasError: true,
message: error.message
hasError: true
}
}
render() {
if (this.state.hasError) {
return this.props.fallback || this.state.message
return this.props.fallback
}
return this.props.children
}
Expand Down Expand Up @@ -259,46 +258,4 @@ describe('useSWR - suspense', () => {
expect(startRenderCount).toBe(2) // fallback + data
expect(renderCount).toBe(1) // data
})

it('should throw an error if key is a falsy value', async () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
jest.spyOn(console, 'error').mockImplementation(() => {})

function Page() {
const { data } = useSWR(null, () => createResponse('SWR'), {
suspense: true
})
return <div>hello, {data}</div>
}
render(
<ErrorBoundary>
<Suspense fallback={<div>fallback</div>}>
<Page />
</Suspense>
</ErrorBoundary>
)

screen.getByText('useSWR requires either key or fetcher with suspense mode')
})

it('should throw an error if fetch is null', async () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
jest.spyOn(console, 'error').mockImplementation(() => {})

function Page() {
const { data } = useSWR('suspense-11', null, {
suspense: true
})
return <div>hello, {data}</div>
}
render(
<ErrorBoundary>
<Suspense fallback={<div>fallback</div>}>
<Page />
</Suspense>
</ErrorBoundary>
)

screen.getByText('useSWR requires either key or fetcher with suspense mode')
})
})

0 comments on commit 5e579e0

Please sign in to comment.