-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
@reduxjs/toolkit: 2.0.1
react-redux: 9.1.0
next: 14.1.0
Hi, recently I've been trying to use the new feature of createSlice in RTK V2 for code splitting, I encountered some uncertainties and errors. I reviewed the documentation but wasn't entirely sure, so I created this issue for clarification.
I created this repository to illustrate my questions, which include three branches: main
, approach-2
, and approach-3
main branch
In this branch, I injected the reducer on pages that require optionalSlice
(index.tsx
, other.tsx
), and used listenerMiddleware to startListening for the required listeners
rootReducer.inject(optionalSlice);
listenerMiddleware.startListening({ actionCreator: optionalIncrement, effect: onOptionalIncrementEffect });
listenerMiddleware.startListening({ actionCreator: optionalDecrement, effect: onOptionalDecrementEffect });
Questions
- Is it okay to use
listenerMiddleware.startListening
for dynamically injecting listener effects, considering that the documentation indicates we should useaddListener
? - Is it okay to write the same logic in two page components (
index.tsx
,other.tsx
)? I am not sure if this is a proper approach. I am concerned it might cause some side effects. Although I tested it myself and it seems fine. - I encountered some TypeScript issues when using
startListening
, but I'm not sure where I went wrong

approach-2 branch
In this branch, not much has been changed. In a real-world app, adding logic to every required page might be impractical. Therefore, I wrote a CheckRequiredResource.tsx
file, which wraps any component that meets the required conditions with InjectRoute
.
Questions
- Just want to check if this approach is okay or not
Note
this approach has no effect on code splitting because CheckRequiredResource still references optionSlice.
you can ignore, I need to find another way
approach-3 branch
In this branch, I updated the InjectRoute
. I used useEffect to inject the reducer and corresponding listener. Additionally, I added a state isInitializeDone
to ensure that the children have the corresponding listener and reducer when rendering.
It is evident that this approach has an impact on SEO.
useEffect(() => {
const unsubscribeFn: any[] = [];
// some condition needed
rootReducer.inject(optionalSlice);
unsubscribeFn.push(dispatch(addListener({ actionCreator: optionalIncrement, effect: onOptionalIncrementEffect })))
unsubscribeFn.push(dispatch(addListener({ actionCreator: optionalDecrement, effect: onOptionalDecrementEffect })))
setIsInitializeDone(true);
return () => {
unsubscribeFn.forEach(fn => fn());
};
}, []);
Questions
- I am uncertain whether it is ok to repeatedly inject the same reducer and listener. If it is allowed, does that mean there is no need to unsubscribe listener and inject the reducer based on conditions (like state is undefined or not)?
- I encountered some TypeScript issues when using
addListener
, but I'm not sure where I went wrong

Note
this approach has no effect on code splitting because CheckRequiredResource still references optionSlice.
you can ignore, I need to find another way
I'm not sure if it's appropriate to post it here. Thank you for your help.