S
S
Sergey Nikolaev2017-03-06 15:04:32
JavaScript
Sergey Nikolaev, 2017-03-06 15:04:32

How to properly update a list in React Redux?

Hello, such a question:
I have a Store:

{
filters: {category: [1,2], tag: [3]},
list:[]
}

There is a reducer and an action:
//Reducer
 case FILTERS_CHANGED:
      return state.merge({filters: state.filters.merge(action.payload)});
//Action
return dispatch => {
  dispatch({type: constants.RESEARCH_FILTERS_CHANGED, payload: filter});
};

From the Filters component, I am modifying the filters object from store with the action above.
How to make request and response write to store.list every time store.filters changes?
In Angular2 I would do something like:
store.filters.subscribe(filters => {
//вызов action для подгрузки данных с бека и добавления их в лист
loadListAction(filters);
});

How to do this correctly in React+Redux?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor, 2017-03-06
@Devastor

1) redux has a subscribe method .
2) Or look for a ready-made plugin. For example: redux-watch , redux-subscribe , redux-changes . Naturally, you can find many other similar plugins and choose which one you need.

A
amiksam, 2017-03-18
@amiksam

I would do it through Redux Middleware.
Add middleware after.js
export default store => next => action => {
const {type} = action
const result = next(action)
switch (type) {
case constants.RESEARCH_FILTERS_CHANGED:
store.dispatch(loadListAction(store.getState( ).filters));
break;
}
return result
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question