Skip to content

Commit e52f609

Browse files
author
ben.durrant
committed
throw if selectState returns undefined in an uninjected slice
1 parent 737d5cc commit e52f609

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

packages/toolkit/src/createSlice.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,20 @@ export function createSlice<
514514
for (const [name, selector] of Object.entries(
515515
options.selectors ?? {}
516516
)) {
517-
cached[name] = (rootState: any, ...args: any[]) =>
518-
selector(
519-
selectState(rootState) ??
520-
(this !== slice ? this.getInitialState() : (undefined as any)),
521-
...args
522-
)
517+
cached[name] = (rootState: any, ...args: any[]) => {
518+
let sliceState = selectState(rootState)
519+
if (typeof sliceState === 'undefined') {
520+
// check if injectInto has been called
521+
if (this !== slice) {
522+
sliceState = this.getInitialState()
523+
} else {
524+
throw new Error(
525+
'selectState returned undefined for an uninjected slice reducer'
526+
)
527+
}
528+
}
529+
return selector(sliceState, ...args)
530+
}
523531
}
524532
selectorCache.set(selectState, cached)
525533
}

0 commit comments

Comments
 (0)