Skip to content

fix: react-hooks/exhaustive-deps warnings #886

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

Merged
merged 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/use-swr-infinite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function useSWRInfinite<Data = any, Error = any>(
// not ready
}

const rerender = useState<boolean>(false)[1]
const [, rerender] = useState<boolean>(false)

// we use cache to pass extra info (context) to fetcher so it can be globally shared
// here we get the key of the fetcher context cache
Expand Down
39 changes: 21 additions & 18 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,27 +302,30 @@ function useSWR<Data = any, Error = any>(
// display the data label in the React DevTools next to SWR hooks
useDebugValue(stateRef.current.data)

const rerender = useState(null)[1]
let dispatch = useCallback((payload: actionType<Data, Error>) => {
let shouldUpdateState = false
for (let k in payload) {
if (stateRef.current[k] === payload[k]) {
continue
}
const [, rerender] = useState(null)
let dispatch = useCallback(
(payload: actionType<Data, Error>) => {
let shouldUpdateState = false
for (let k in payload) {
if (stateRef.current[k] === payload[k]) {
continue
}

stateRef.current[k] = payload[k]
if (stateDependencies.current[k]) {
shouldUpdateState = true
stateRef.current[k] = payload[k]
if (stateDependencies.current[k]) {
shouldUpdateState = true
}
}
}

if (shouldUpdateState || config.suspense) {
// if component is unmounted, should skip rerender
// if component is not mounted, should skip rerender
if (unmountedRef.current || !initialMountedRef.current) return
rerender({})
}
}, [])
if (shouldUpdateState || config.suspense) {
// if component is unmounted, should skip rerender
// if component is not mounted, should skip rerender
if (unmountedRef.current || !initialMountedRef.current) return
rerender({})
}
},
[config.suspense]
Copy link
Member

Choose a reason for hiding this comment

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

We need to ignore this dependence because we don't allow the suspense option to change during the lifecycle, it has to be static (otherwise it would be difficult to track the internal state).

Maybe we should warn the user when they try to change the option.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@shuding Thank you for your feedback. I've disabled the warning by the eslint-disable comment instead of adding it to the deps array.

Maybe we should warn the user when they try to change the option.

That's a good idea. I think the warning isn't required on a production environment, but swr doesn't have a way to print warnings only for a development build. I'll propose it later.

)

// error ref inside revalidate (is last request errored?)
const unmountedRef = useRef(false)
Expand Down