Skip to content

Commit a1a32d1

Browse files
authored
Merge pull request #2064 from reduxjs/warning-stack
2 parents 4a7e129 + bc328d9 commit a1a32d1

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/hooks/useSelector.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ export function createSelectorHook(context = ReactReduxContext): UseSelector {
9999
) {
100100
const toCompare = selector(state)
101101
if (!equalityFn(selected, toCompare)) {
102+
let stack: string | undefined = undefined
103+
try {
104+
throw new Error()
105+
} catch (e) {
106+
;({ stack } = e as Error)
107+
}
102108
console.warn(
103109
'Selector ' +
104110
(selector.name || 'unknown') +
@@ -108,6 +114,7 @@ export function createSelectorHook(context = ReactReduxContext): UseSelector {
108114
state,
109115
selected,
110116
selected2: toCompare,
117+
stack,
111118
}
112119
)
113120
}
@@ -120,11 +127,18 @@ export function createSelectorHook(context = ReactReduxContext): UseSelector {
120127
) {
121128
// @ts-ignore
122129
if (selected === state) {
130+
let stack: string | undefined = undefined
131+
try {
132+
throw new Error()
133+
} catch (e) {
134+
;({ stack } = e as Error)
135+
}
123136
console.warn(
124137
'Selector ' +
125138
(selector.name || 'unknown') +
126139
' returned the root state when called. This can lead to unnecessary rerenders.' +
127-
'\nSelectors that return the entire state are almost certainly a mistake, as they will cause a rerender whenever *anything* in state changes.'
140+
'\nSelectors that return the entire state are almost certainly a mistake, as they will cause a rerender whenever *anything* in state changes.',
141+
{ stack }
128142
)
129143
}
130144
}

test/hooks/useSelector.spec.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ describe('React', () => {
805805
}),
806806
selected: expect.any(Number),
807807
selected2: expect.any(Number),
808+
stack: expect.any(String),
808809
})
809810
)
810811
})
@@ -920,7 +921,10 @@ describe('React', () => {
920921
)
921922

922923
expect(consoleSpy).toHaveBeenCalledWith(
923-
expect.stringContaining('returned the root state when called.')
924+
expect.stringContaining('returned the root state when called.'),
925+
expect.objectContaining({
926+
stack: expect.any(String),
927+
})
924928
)
925929
})
926930
})

0 commit comments

Comments
 (0)