0

I was using redux-saga in my application. And use saga to set the state of my redux. But one thing i am experiencing now is the application will hang if I use saga to update the redux state with a different value from its existing state

Lets say, in my redux there is state name locale. first i set it to en, and create an action CHANGE_LOCALE to change the locale state to zh later.

reducer.js

const initialState = fromJS({
  locale: 'en' <- set initial state to en
});

function languageProviderReducer(state = initialState, action) {
  switch (action.type) {
    case CHANGE_LOCALE:
      return state
        .set('locale', action.locale);
    default:
      return state;
  }
}

saga.js

export function* loadProfile() {
  const profileURL = 'profile';
  const profile = yield axios.get(profileURL)
    // .then((res) => (res.data))
    .then((data) => (data.data));

  yield put({
    type: ACTION1,
    payload: profile,
  });
  console.log('finish action one');

  yield put({
    type: CHANGE_LOCALE,
    locale: 'zh', <- change to zh using saga
  });
  console.log('finish action two');

  yield put({
    type: ACTION3,
    payload: profile,
  });
  console.log('finish action three');
}
export default function* defaultSaga() {
  yield takeLatest(constants.LOAD_PROFILE, loadProfile);
}

So when I run the app, the console.log is just showing finish action one. Meaning it is hanging after changing the locale state of Redux .

( I use Redux Dev Tool to check and it actually has changed the locale state to'zh', -> it actually finish the action two however , it seems like the saga dont know what to do next )

p/s: The programm will run sucessfully after hanging for 2 or 3 minutes , so its like saga is stuck and then keep retry after a few minutes and succeed Any help please ??

Jake Lam
  • 3,254
  • 3
  • 25
  • 44
  • `put` effect silents errors that happen during its execution. I am assuming there is some error while rerendering your view. Try to call `store.dispatch({type:CHANGE_LOCALE, locale: 'zh'})` from console to see if your app throws an err. – Martin Kadlec Jun 27 '18 at 20:00
  • https://github.com/redux-saga/redux-saga/issues/550 – Martin Kadlec Jun 27 '18 at 20:01
  • This is something that is probably going to change in redux-saga@1.0.0 (see breaking changes in https://github.com/redux-saga/redux-saga/releases/tag/v1.0.0-beta.0) – Martin Kadlec Jun 27 '18 at 20:04
  • @MartinKadlec I was also tried to include the catch block to get the error, but it seems like there was no error there , it just somehow get cancelled after run the CHANGE_LOCALE, . – Jake Lam Jul 06 '18 at 04:24
  • @MartinKadlec I also tried to change the redux-saga version but i dont think this is the reason – Jake Lam Jul 06 '18 at 04:25

0 Answers0