-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: revalidate with initialData when changing the key #961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 655fec4:
|
The test has been passed in this PR, but I think this shouldn't be passed. it('should revalidate even if initialData is provided', async () => {
const fetcher = async (key) => {
sleep(100);
return key
}
function Page() {
const [key, setKey] = useState("initial-data-returns-empty-string")
const { data } = useSWR(key, fetcher, {
initialData: 'Initial'
})
return (
<div onClick={() => setKey('initial-data-returns-empty-string-update')}>
hello, {data}
</div>
)
}
const { container } = render(<Page />)
// render with the initial data
await screen.findByText('hello, Initial')
await act(() => sleep(1))
fireEvent.focus(window)
await screen.findByText('hello, initial-data-returns-empty-string')
// change the key
await act(() => sleep(1))
fireEvent.click(container.firstElementChild)
await act(() => sleep(10))
// THIS SHOULD BE FAILED, BUT IT'S PASSED
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"hello, Initial"`)
// render with data the fetcher returns
await screen.findByText('hello, initial-data-returns-empty-string-update')
}) You can also see the behavior on https://codesandbox.io/s/swr-basic-forked-qjnnp?file=/src/App.js. |
b174b9a
to
1fd4569
Compare
test/use-swr-integration.test.tsx
Outdated
|
||
// a request is still in flight | ||
await act(() => sleep(10)) | ||
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"loading"`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what the expected value is.
But I expected SWR returns undefined
as the return value of data
when revalidating. But SWR returns the initialData
Is it an expected behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah initialData
works like a fallback value currently. I think we need to better name for it, maybe just fallback
. And for the real initialData
, we need a completely new API, which works like a prefill. (also cc @huozhi)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems to be great 👍 fallback
would be more intuitive than initialData
for the behavior.
a8e1b01
to
655fec4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix looks good, thanks!
Thank you! |
This fixes #947.
SWR seems not to revalidate when having
initialData
.This PR doesn't fix the above that is also mentioned on #947.
I'll investigate it as another issue.