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

fix(react-table): fix improper state update on unmounted component #5879

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
17 changes: 15 additions & 2 deletions packages/react-table/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ export function useReactTable<TData extends RowData>(
// By default, manage table state here using the table's initial state
const [state, setState] = React.useState(() => tableRef.current.initialState)

// Keep track of whether the component on which this table is referenced is
// mounted to avoid setting state on an unmounted component
const isMountedRef = React.useRef(false)
React.useEffect(() => {
isMountedRef.current = true

return () => {
isMountedRef.current = false
}
}, [])

// Compose the default state above with any user state. This will allow the user
// to only control a subset of the state if desired.
tableRef.current.setOptions(prev => ({
Expand All @@ -85,8 +96,10 @@ export function useReactTable<TData extends RowData>(
// Similarly, we'll maintain both our internal state and any user-provided
// state.
onStateChange: updater => {
setState(updater)
options.onStateChange?.(updater)
if (isMountedRef.current) {
setState(updater)
options.onStateChange?.(updater)
}
},
}))

Expand Down