Create a selector that guarantees that the slices injected will have a defined value when selector is run.
const selectBooleanWithoutInjection = (state: RootState) => state.boolean;
// ^? boolean | undefined
const selectBoolean = rootReducer.inject(booleanSlice).selector((state) => {
// if action hasn't been dispatched since slice was injected, this would usually be undefined
// however selector() uses a Proxy around the first parameter to ensure that it evaluates to the initial state instead, if undefined
return state.boolean;
// ^? boolean
})
If the reducer is nested inside the root state, a selectState callback can be passed to retrieve the reducer's state.
export interface LazyLoadedSlices {};
export const innerReducer = combineSlices(stringSlice).withLazyLoadedSlices<LazyLoadedSlices>();
export const rootReducer = combineSlices({ inner: innerReducer });
export type RootState = ReturnType<typeof rootReducer>;
// elsewhere
declare module "./reducer.ts" {
export interface LazyLoadedSlices extends WithSlice<typeof booleanSlice> {}
}
const withBool = innerReducer.inject(booleanSlice);
const selectBoolean = withBool.selector(
(state) => state.boolean,
(rootState: RootState) => state.inner
);
// now expects to be passed RootState instead of innerReducer state
Value passed to selectorFn will be a Proxy - use selector.original(proxy) to get original state value (useful for debugging)
const injectedReducer = rootReducer.inject(booleanSlice);
const selectBoolean = injectedReducer.selector((state) => {
console.log(injectedReducer.selector.original(state).boolean) // possibly undefined
return state.boolean
})
Create a selector that guarantees that the slices injected will have a defined value when selector is run.
const selectBooleanWithoutInjection = (state: RootState) => state.boolean;
// ^? boolean | undefined
const selectBoolean = rootReducer.inject(booleanSlice).selector((state) => {
// if action hasn't been dispatched since slice was injected, this would usually be undefined
// however selector() uses a Proxy around the first parameter to ensure that it evaluates to the initial state instead, if undefined
return state.boolean;
// ^? boolean
})
Value passed to selectorFn will be a Proxy - use selector.original(proxy) to get original state value (useful for debugging)
const injectedReducer = rootReducer.inject(booleanSlice);
const selectBoolean = injectedReducer.selector((state) => {
console.log(injectedReducer.selector.original(state).boolean) // undefined
return state.boolean
})
Create a selector that guarantees that the slices injected will have a defined value when selector is run.
const selectBooleanWithoutInjection = (state: RootState) => state.boolean;
// ^? boolean | undefined
const selectBoolean = rootReducer.inject(booleanSlice).selector((state) => {
// if action hasn't been dispatched since slice was injected, this would usually be undefined
// however selector() uses a Proxy around the first parameter to ensure that it evaluates to the initial state instead, if undefined
return state.boolean;
// ^? boolean
})
If the reducer is nested inside the root state, a selectState callback can be passed to retrieve the reducer's state.
interface LazyLoadedSlices {};
const innerReducer = combineSlices(stringSlice).withLazyLoadedSlices<LazyLoadedSlices>();
const rootReducer = combineSlices({ inner: innerReducer });
type RootState = ReturnType<typeof rootReducer>;
// elsewhere
declare module "./reducer.ts" {
interface LazyLoadedSlices extends WithSlice<typeof booleanSlice> {}
}
const withBool = innerReducer.inject(booleanSlice);
const selectBoolean = withBool.selector(
(state) => state.boolean,
(rootState: RootState) => state.inner
);
// now expects to be passed RootState instead of innerReducer state
Value passed to selectorFn will be a Proxy - use selector.original(proxy) to get original state value (useful for debugging)
const injectedReducer = rootReducer.inject(booleanSlice);
const selectBoolean = injectedReducer.selector((state) => {
console.log(injectedReducer.selector.original(state).boolean) // possibly undefined
return state.boolean
})
Returns the unproxied state. Useful for debugging.
Inject a slice.
Accepts an individual slice, RTKQ API instance, or a "slice-like" { reducerPath, reducer } object.
rootReducer.inject(booleanSlice)
rootReducer.inject(baseApi)
rootReducer.inject({ reducerPath: 'boolean' as const, reducer: newReducer }, { overrideExisting: true })
Optional
config: InjectConfigInject a slice.
Accepts an individual slice, RTKQ API instance, or a "slice-like" { reducerPath, reducer } object.
rootReducer.inject(booleanSlice)
rootReducer.inject(baseApi)
rootReducer.inject({ reducerPath: 'boolean' as const, reducer: newReducer }, { overrideExisting: true })
Optional
config: InjectConfigProvide a type for slices that will be injected lazily.
One way to do this would be with interface merging:
export interface LazyLoadedSlices {}
export const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<LazyLoadedSlices>();
// elsewhere
declare module './reducer' {
export interface LazyLoadedSlices extends WithSlice<typeof booleanSlice> {}
}
const withBoolean = rootReducer.inject(booleanSlice);
// elsewhere again
declare module './reducer' {
export interface LazyLoadedSlices {
customName: CustomState
}
}
const withCustom = rootReducer.inject({ reducerPath: "customName", reducer: customSlice.reducer })
A reducer that allows for slices/reducers to be injected after initialisation.