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 3 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
41 changes: 23 additions & 18 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,27 +302,32 @@ 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 isn't allowed to change during the lifecycle
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)

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