admin管理员组文章数量:1402852
I have a react/redux app where I have some filtering and searching. Because of how the API's are set up, I have to do a little ground work in between receiving the filters and then sending the new search query. So I have a small reference map of the active filters that updates each time I check or uncheck a filter. The problem is, I would like the action to run that updates the filter, and then the action to call the server with the new filter parameters after that, and I am unsure how this workflow is suppose to go in redux.
So I call the action, then it hits the reducer with an updated state like so -
case actions.TOGGLE_FILTER:
return toggleFilter(state, action);
var toggleFilter = function(state, action){
var currentFilters = state.toJS().activeFilters || {};
var removeFilterFromActive = function(item) {
if(item != action.filter.search){
return;
}
}
//already exists, remove from list (toggle off)
if (currentFilters[action.filterGroup.parentSearch] && currentFilters[action.filterGroup.parentSearch].indexOf(action.filter.search) > -1) {
var itemIndex = currentFilters[action.filterGroup.parentSearch].indexOf(action.filter.search);
currentFilters[action.filterGroup.parentSearch].splice(itemIndex, 1);
} else {
//add to obj
var newObj = {};
if (currentFilters[action.filterGroup.parentSearch]) {
currentFilters[action.filterGroup.parentSearch].push(action.filter.search);
} else {
newObj[action.filterGroup.parentSearch] = [];
newObj[action.filterGroup.parentSearch].push(action.filter.search);
_.extend(currentFilters, newObj);
}
}
return state.set('activeFilters', fromJS(currentFilters));
};
So this assembles my activeFilters
state, seems to be working ok for now. But the part I can't figure out, is how to have the call the server with using my updated activeFilters. Right now I am just calling this action from the ponent it is being used in.
Is there a way to chain, or promise, or dispatch another action inside the reducer when this one has pleted? Any advice in how to approach this would be much appreciated. Thanks!
I have a react/redux app where I have some filtering and searching. Because of how the API's are set up, I have to do a little ground work in between receiving the filters and then sending the new search query. So I have a small reference map of the active filters that updates each time I check or uncheck a filter. The problem is, I would like the action to run that updates the filter, and then the action to call the server with the new filter parameters after that, and I am unsure how this workflow is suppose to go in redux.
So I call the action, then it hits the reducer with an updated state like so -
case actions.TOGGLE_FILTER:
return toggleFilter(state, action);
var toggleFilter = function(state, action){
var currentFilters = state.toJS().activeFilters || {};
var removeFilterFromActive = function(item) {
if(item != action.filter.search){
return;
}
}
//already exists, remove from list (toggle off)
if (currentFilters[action.filterGroup.parentSearch] && currentFilters[action.filterGroup.parentSearch].indexOf(action.filter.search) > -1) {
var itemIndex = currentFilters[action.filterGroup.parentSearch].indexOf(action.filter.search);
currentFilters[action.filterGroup.parentSearch].splice(itemIndex, 1);
} else {
//add to obj
var newObj = {};
if (currentFilters[action.filterGroup.parentSearch]) {
currentFilters[action.filterGroup.parentSearch].push(action.filter.search);
} else {
newObj[action.filterGroup.parentSearch] = [];
newObj[action.filterGroup.parentSearch].push(action.filter.search);
_.extend(currentFilters, newObj);
}
}
return state.set('activeFilters', fromJS(currentFilters));
};
So this assembles my activeFilters
state, seems to be working ok for now. But the part I can't figure out, is how to have the call the server with using my updated activeFilters. Right now I am just calling this action from the ponent it is being used in.
Is there a way to chain, or promise, or dispatch another action inside the reducer when this one has pleted? Any advice in how to approach this would be much appreciated. Thanks!
Share Improve this question asked Dec 29, 2015 at 3:48 ajmajmajmaajmajmajma 14.2k25 gold badges83 silver badges138 bronze badges1 Answer
Reset to default 8Reducers should be pure with no side effects, so you don't want your reducers firing requests to the server or issuing additional actions.
If you are using redux-thunk, then you can dispatch functions as actions. These functions can examine the state of the store. These functions can themself dispatch multiple regular actions. And, if you aren't doing any sort of batching of your Redux updates, they can examine the store after they dispatch an action and then do more stuff.
With the above in mind, you can do something like this:
function createToggleFilterAndQueryServerAction(filterGroup, filter) {
return (dispatch, getState) => {
// first dispatch the filter toggle
// if you do not have any batching middleware, this
// should run the reducers and update the store
// synchronously
dispatch(createToggleFilter(filterGroup, filter));
// Now get the updated filter from the store
const activeFilter = getState().activeFilter;
// now query the server
callServer(activeFilter).then(result => {
// Now dispatch an action with the result from the server
dispatch(createServerResponseAction(result));
});
};
}
Usage:
dispatch(createToggleFilterAndQueryServerAction(..., ...));
本文标签: javascriptReactReduxchain or promise in reducerStack Overflow
版权声明:本文标题:javascript - ReactRedux, chain or promise in reducer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744291431a2599132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论