V
V
vixxxx22018-02-16 17:18:06
React
vixxxx2, 2018-02-16 17:18:06

How to subscribe in Redux?

I use redux-thunk middlware
Actually, there are some components that are standalone and you need to track their changes
somehow Produce conditions in them I don’t want modularity, everything is fine
But you need to track the action somehow
For the time being, I just made a small middlware, but it seems to me that this is the way to nowhere

const boardMiddleware = store => next => action => {
  if (action.type.includes('CLOSE_MODAL') && (
    action.payload === 'modal1' ||
    action.payload === 'modal2' ||
    action.payload === 'modal3'
  )) {

    switch (action.payload) {
      case 'modal1':
        store.dispatch(boardAction({ modal1: true }));
        if(!store.getState().auth) {
          store.dispatch(openModalAction('modal3'));
        }
        break;
      case 'modal2':
        store.dispatch(onboardAction({ modal2: true }));
        if(!store.getState().auth) {
          store.dispatch(openModalAction('modal3'));
        }
        break;
      case 'modal3':
        store.dispatch(boardAction({ modal3: true }));
        break;
    }

    return next(action);
  } else {
    return next(action);
  }
};

What are the exits or libraries for this
? Thank you all

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
davidnum95, 2018-02-16
@davidnum95

Redux-saga

A
Alexander Petrov, 2018-02-16
@dzhiriki

If I understand correctly, then the essence of the question is how to call another one by one action?
The problem is in the question itself. You don't need to call another action at all. If you always have paired actions, then you should simply subscribe to the same action in both reducers. The link doesn't have to be 1:1.
And in fact, it is more correct in terms of state consistency. In the case when you have two actions (the first modal closes separately, then the second one opens separately), the application is in a moment of time when the modal is not open alone. Although from the point of view of business logic, such a state does not exist. In this case, this is not a problem, but in general it can lead to problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question