react-boilerplate: SAGA: Cannot read property cancel of undefined
React Boilerplate
Issue Type
- Bug (https://github.com/react-boilerplate/react-boilerplate/blob/master/.github/CONTRIBUTING.md#bug-reports)
- Feature (https://github.com/react-boilerplate/react-boilerplate/blob/master/.github/CONTRIBUTING.md#feature-requests)
Description
Hi there,
Just wanted to point out that the following issue has not been resolved yet: https://github.com/react-boilerplate/react-boilerplate/issues/2021
@kai23 solution of adding ‘&& descriptor.task’ on line 75 worked for me.
export function ejectSagaFactory(store, isValid) {
return function ejectSaga(key) {
if (!isValid) checkStore(store);
checkKey(key);
if (Reflect.has(store.injectedSagas, key)) {
const descriptor = store.injectedSagas[key];
if (descriptor.mode && descriptor.mode !== DAEMON && descriptor.task) {
descriptor.task.cancel();
// Clean up in production; in development we need `descriptor.saga` for hot reloading
if (process.env.NODE_ENV === 'production') {
// Need some value to be able to detect `ONCE_TILL_UNMOUNT` sagas in `injectSaga`
store.injectedSagas[key] = 'done'; // eslint-disable-line no-param-reassign
}
}
}
};
}
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 4
- Comments: 19 (6 by maintainers)
i did the following in injectSaga
never have duplicated injected sagas this way
I ran into similar issue, and this was because of race condition between injecting saga and ejecting them. componentWillUnMount is used for ejecting the saga which is a asyncrounous in React 16. Because of this you may inject a new instance of saga to the same key making the previous saga unaccessible. This should be fixed by keeping sagas mode DAEMON by default and should be fixed with #2430
I too have this issue in
productionmode. The issue occurs when I switch languages usingreact-intl. Some of my components useInjectIntldirectly in order to translate strings (rather than HTML elements). See API docs for more details.The root of the problem lies within the React component lifecycle.
InjectIntlwill wrap my component.injectSagaalso wraps my component. Since theejectSagalogic is run oncomponentWillUnmount, the logic is asynchronous. In my particular case, changing the language within my app triggers an update down to my component that is wrapped in bothInjectIntlandinjectSaga.The resulting order of execution is as follows:
keyDebugging output at this point is as follows:
InjectIntlwrapped component mount logic applies FIRST (which re-injects the saga)Debugging output for this action is as follows:
At this point, you may be able to see the obvious problem… the saga was already injected (and had not yet ejected), so re-injecting the already existing saga does nothing.
InjectIntlwrapped component unmount logic applies SECOND (which ejects the saga)Debugging output for this action is as follows:
From this point , any attempt to run the saga will result in the following exception:
Closing for lack of activity + the APIs have changed so much I’m not sure these issues are still relevant. If you need to report new bugs with reducer or saga injection, please open an issue in our sister project redux-injectors. Thanks 🙏
@tatemz I just face similar scenario with your few day ago. I elaborate here maybe could be someone ref.
In my case, when
container/App/index.jsmounted,componentDidMountwilldispatch(initApp())action to fetchuserConfig. And weset language localeAccordinguserConfig, and things happenAccording to my understanding:
saga A(1)is injected for mountPage A(1)dispatch(initApp())set language locale<IntlProvider />re-mount (due toreact-boilerplatedesignlocaleaselement's key)Page Aremount(and due to
React 16 life change)saga A(2)is injected forPage A(2)WillMountsaga A(1)is ejected forPage A(1)WillUnMountat this point,
saga A(2)still work perfectly.and We find out things broken by following scenario
Page B(saga A(2)didn’t cancel, cusredux-sagathink it not running. without any console.error)Page Athensaga A(3)injectedreact-intl
further explain, In my view, I don’t think this design is a bug
redux-saga
Steps to reproduce
FYI