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

Add stack to dev mode checks #2064

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion src/hooks/useSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ export function createSelectorHook(context = ReactReduxContext): UseSelector {
) {
const toCompare = selector(state)
if (!equalityFn(selected, toCompare)) {
let stack: string | undefined = undefined
try {
throw new Error()
} catch (e) {
;({ stack } = e as Error)
}
console.warn(
'Selector ' +
(selector.name || 'unknown') +
Expand All @@ -108,6 +114,7 @@ export function createSelectorHook(context = ReactReduxContext): UseSelector {
state,
selected,
selected2: toCompare,
stack,
}
)
}
Expand All @@ -120,11 +127,18 @@ export function createSelectorHook(context = ReactReduxContext): UseSelector {
) {
// @ts-ignore
if (selected === state) {
let stack: string | undefined = undefined
try {
throw new Error()
} catch (e) {
;({ stack } = e as Error)
}
console.warn(
'Selector ' +
(selector.name || 'unknown') +
' returned the root state when called. This can lead to unnecessary rerenders.' +
'\nSelectors that return the entire state are almost certainly a mistake, as they will cause a rerender whenever *anything* in state changes.'
'\nSelectors that return the entire state are almost certainly a mistake, as they will cause a rerender whenever *anything* in state changes.',
{ stack }
)
}
}
Expand Down
6 changes: 5 additions & 1 deletion test/hooks/useSelector.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ describe('React', () => {
}),
selected: expect.any(Number),
selected2: expect.any(Number),
stack: expect.any(String),
})
)
})
Expand Down Expand Up @@ -920,7 +921,10 @@ describe('React', () => {
)

expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('returned the root state when called.')
expect.stringContaining('returned the root state when called.'),
expect.objectContaining({
stack: expect.any(String),
})
)
})
})
Expand Down
Loading