4

I have a react web app that uses redux, connected-react-router, redux saga and redux persist and HMR with react-hot-loader and webpack. After doing a major update to most of the packages I noticed that the sagas are not entered / executed.

The current versions of the relevant packages are: "react": "^16.7.0", "react-redux": "^6.0.0", "redux": "^4.0.1", "redux-persist": "5.6.12", "redux-saga": "^1.0.1", "react-hot-loader": "^4.6.5", "connected-react-router": "^6.2.2", "webpack": "^4.29.3".

I have tried reverting the HMR implementation from v4 to something lower but I am confident that is working. I also thought it may be the connected-react-router implementation but I'm also confident on that now (but I will show both for reference). I am guessing it is something around my redux store config but I guess if I knew I wouldn't be asking for help.

index.js file (application entry point)

import React from 'react';
import ReactDOM from 'react-dom';
import { PersistGate } from 'redux-persist/integration/react';
import { Provider } from 'react-redux';
import App from './components/App';
import store, { persistor } from './store/config';

const render = (Component) => {
  ReactDOM.render(
    <Provider store={store}>
      <PersistGate persistor={persistor}>
        <Component />
      </PersistGate>
    </Provider>,
    document.getElementById('app'),
  );
};

render(App);

root reducer:

import { combineReducers } from 'redux';
import { connectRouter } from 'connected-react-router';
import { stateKeys } from '../types';
import authReducer from './auth/authReducer';

export default history => combineReducers({
  [stateKeys.ROUTER]: connectRouter(history),
  [stateKeys.AUTH]: authReducer,
});

root saga:

import watchAuthentication from './auth/sagas';

const root = function* rootSaga() {
  yield [
    watchAuthentication(),
  ];
};

export default root;

App.js (only relevant bits):

import { hot } from 'react-hot-loader';
class App extends React.Component {
...
}
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(App));

store config:

import {
  applyMiddleware,
  compose,
  createStore,
} from 'redux';
import createSagaMiddleware from 'redux-saga';
import { createMigrate, persistStore, persistReducer } from 'redux- 
persist';
import storage from 'redux-persist/lib/storage';
import reduxImmutableStateInvariant from 'redux-immutable-state- 
invariant';
import { createBrowserHistory } from 'history';
import { routerMiddleware } from 'connected-react-router';
import { manifest } from '../manifest';
import rootReducer from '../rootReducer';
import sagas from '../rootSaga';
import { stateKeys } from '../../types';


// persistence config
const persistConfig = {
  key: 'root',
  whitelist: [
    stateKeys.MANIFEST,
    stateKeys.VERSION,
  ],
  storage,
  migrate: createMigrate(manifest),
};

// Create and export the history object
export const history = createBrowserHistory();


// Middlewares setup
const reactRouterMiddleware = routerMiddleware(history);
const sagaMiddleware = createSagaMiddleware();

const middlewares = [];

// during development: enforce immutability and provide extended support for redux debugging tools.
let composeEnhancers = compose;

if (process.env.NODE_ENV === 'development') {
  middlewares.push(reduxImmutableStateInvariant());
  composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || 
composeEnhancers; // eslint-disable-line no-underscore-dangle, max-len
}

middlewares.push(sagaMiddleware, reactRouterMiddleware);

// create the redux store
const initialState = undefined;

const store = createStore(
  persistReducer(persistConfig, rootReducer(history)),
  initialState,
  composeEnhancers(applyMiddleware(...middlewares)),
);

// hot module replacement config
if (process.env.NODE_ENV === 'development' && module.hot) {
  module.hot.accept('../rootReducer', () => {
    const nextReducer = require('../rootReducer').default; // eslint-disable-line global-require
    store.replaceReducer(persistReducer(persistConfig, 
nextReducer(history)));
  });
}

// run the saga middleware
sagaMiddleware.run(sagas);

export const persistor = persistStore(store);
export default store;

Auth Saga:

import {
  call,
  take,
  takeLatest,
} from 'redux-saga/effects';
import * as actions from '../authActions';
import config from '../../../mockData/mock-config';

// Use mocked auth flow if mocked authentication is enabled in mock- 
   config.js.
   // If mocked auth is used, you can change the user object in the 
    mock-config.js
    const { authenticateFlow, signOutFlow } = (config.enabled && 
    config.mockAuthentication) ? require('./mockedAuthFlow') : 
    require('./authFlow');

console.log('Outside watchAuthentication: sagas are not 
running...why?');


export default function* watchAuthentication() {
  while(true) { // eslint-disable-line
    try {
      console.log('Inside watchAuthentication... we never get here, 
why? ');

      const loginAction = yield takeLatest(`${actions.login}`);
      yield call(authenticateFlow, loginAction);

      const signOutAction = yield take(`${actions.loginSignOut}`);

      yield call(signOutFlow, signOutAction);
    } catch (e) {
      console.warn('login flow failed');
    }
  }
}

I would expect the console log inside the watchAuthentication to run but it never does. I believe the issue is around the store config but at this point I'm guessing and grasping at straws because I don't know where to look. I know this is a complex question and I appreciate any help anyone can provide. Thanks in advance!!

cbutler
  • 729
  • 3
  • 10
  • 24
  • I've noticed going from redux-saga "redux-saga": "^0.15.6" to "redux-saga": "^1.0.1" is causing the breaking change but no idea why. I will continue to investigate in case someone else is experiencing this issue as well. – cbutler Feb 19 '19 at 12:13
  • did you ever figure out what caused your watch to stop working? – Plague May 08 '20 at 17:13
  • I did, I will add an answer – cbutler May 09 '20 at 20:46

1 Answers1

0

The issue to this issue with the upgrade of redux saga was in the root saga. My solution was to use yield all as follows:

import { all } from 'redux-saga/effects';
import watchAuthentication from './auth/sagas';

const root = function* rootSaga() {
  yield all([
    watchAuthentication(),
  ]);
};

export default root;

Hope this helps someone out.

cbutler
  • 467
  • 4
  • 20