Skip to content
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

Wrap side effects to fix new warnings in React 16.13 #317

Merged
merged 1 commit into from Mar 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 24 additions & 21 deletions src/use-swr-pages.tsx
Expand Up @@ -149,29 +149,32 @@ export function useSWRPages<OffsetType = any, Data = any, Error = any>(
pageSWRs[id].error !== swr.error ||
pageSWRs[id].revalidate !== swr.revalidate
) {
setPageSWRs(swrs => {
const _swrs = [...swrs]
_swrs[id] = {
data: swr.data,
error: swr.error,
revalidate: swr.revalidate,
isValidating: swr.isValidating,
mutate: swr.mutate
// hoist side effects: setPageSWRs and setPageOffsets -- https://reactjs.org/blog/2020/02/26/react-v16.13.0.html#warnings-for-some-updates-during-render
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// hoist side effects: setPageSWRs and setPageOffsets -- https://reactjs.org/blog/2020/02/26/react-v16.13.0.html#warnings-for-some-updates-during-render

Okay to delete this comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to keep it! :)

setTimeout(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to hoist is useEffect()s, possibly with dependency arrays to take the place of the if clauses below.

setPageSWRs(swrs => {
const _swrs = [...swrs]
_swrs[id] = {
data: swr.data,
error: swr.error,
revalidate: swr.revalidate,
isValidating: swr.isValidating,
mutate: swr.mutate
}
return _swrs
})
if (typeof swr.data !== 'undefined') {
// set next page's offset
const newPageOffset = SWRToOffset(swr, id)
if (pageOffsets[id + 1] !== newPageOffset) {
setPageOffsets(arr => {
const _arr = [...arr]
_arr[id + 1] = newPageOffset
cache.set(pageOffsetKey, _arr)
return _arr
})
}
}
return _swrs
})
if (typeof swr.data !== 'undefined') {
// set next page's offset
const newPageOffset = SWRToOffset(swr, id)
if (pageOffsets[id + 1] !== newPageOffset) {
setPageOffsets(arr => {
const _arr = [...arr]
_arr[id + 1] = newPageOffset
cache.set(pageOffsetKey, _arr)
return _arr
})
}
}
}
return swr
}
Expand Down