【发布时间】:2020-08-04 11:55:32
【问题描述】:
我有一个 react-redux 应用程序,其中存在某个 action-creator(函数获取任何标题以过滤对象列表):
export const filterTests = (title) => dispatch => {
axios.get(`/api/tests/?title=${title}`)
.then(res => {
dispatch({
type: GET_TESTS,
payload: res.data
});
dispatch({
type: FILTER_TESTS,
payload: { title }
});
})
.catch(err => console.log(err))
}
还有一个reducer:
export default function (state = initialState, action) {
switch (action.type) {
case GET_TESTS:
return {
...state,
tests: action.payload,
};
case FILTER_TESTS:
return {
...state,
tests: state.tests.filter((test) => test.title.includes(action.payload.title)),
};
default:
return state;
}
}
它有效,但我认为最好将过滤逻辑移到其他地方(特别是如果我想使操作复杂化):
tests: state.tests.filter((test) => test.title.toLowerCase().includes(action.payload.title.toLowerCase()))
把这个逻辑转移到哪里更好?
【问题讨论】:
标签: javascript reactjs redux react-redux redux-thunk