Skip to content

Commit

Permalink
Merge pull request reduxjs#2064 from reduxjs/warning-stack
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Aug 26, 2023
2 parents 4a7e129 + bc328d9 commit a1a32d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/hooks/useSelector.ts
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
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

0 comments on commit a1a32d1

Please sign in to comment.