[英]Why app redux have error in this reducer?
Why app redux have error in this reducer?为什么应用程序 redux 在此减速器中有错误? what have I not done?我没有做什么? and where exactly?how to fix it?到底在哪里?如何解决? Tell me please Tell me please reducer:请告诉我 请告诉我减速器:
import { searchFilter } from '../components/app';
function reducer (state = {}, action) {
switch (action.type) {
case 'SET_SHIFT':
return Object.assign({} {
shift: action.shift
});
case 'SET_SEARCH':
return Object.assign({} {
search: action.search.toLowerCase()
});
case 'RUN_FILTER':
return Object.assign({} {
shift: action.shift || state.shift,
search: action.search || state.search,
filteredData: searchFilter(state.search, state.data[state.shift])
});
case 'LOAD_DATA_START':
return Object.assign({}{
day: action.day
});
case 'LOAD_DATA_END':
return Object.assign({} {
data: action.data,
shift: Object.keys(action.data)[0],
filteredData: searchFilter(state.search, state.data[state.shift])
});
}
}
export default reducer;
The main problem I see is you do not return the new state from your reducer.我看到的主要问题是您没有从减速器返回新的 state。
A reducer should be a pure function wich, given a state and an action, returns a new state.减速器应该是纯 function,给定一个 state 和一个动作,返回一个新的 state。
put return state
at the bottom of your reducer.将return state
放在减速器的底部。
There's a lot going on in your example, I'd strip it back a bit (especially getting rid of middleware and thunk), get a handle on the basic Redux model and then build it out again.在您的示例中发生了很多事情,我将其剥离一点(尤其是摆脱中间件和 thunk),掌握基本的 Redux model 然后重新构建它。
Your reducer should look something like this:你的减速器应该是这样的:
import { searchFilter } from '../components/app';
function reducer (state = {}, action) {
switch (action.type) {
case 'SET_SHIFT':
//no need for break on return
return Object.assign({}, state, {
shift: action.shift
});
case 'SET_SEARCH':
return Object.assign({}, state, {
search: action.search.toLowerCase()
});
case 'RUN_FILTER':
return Object.assign({}, state, {
shift: action.shift || state.shift,
search: action.search || state.search,
filteredData: searchFilter(state.search, state.data[state.shift])
});
case 'LOAD_DATA_START':
return Object.assign({}, state, {
day: action.day
});
case 'LOAD_DATA_END':
return Object.assign({}, state, {
data: action.data,
shift: Object.keys(action.data)[0],
filteredData: searchFilter(state.search, state.data[state.shift])
});
//then you definitely need to have this
default:
return state;
}
}
export default reducer;
Can I recommend you put some console.log statements in your reducer so you can see the data is being put into the Redux store as expected?我是否可以建议您在减速器中放置一些 console.log 语句,以便您可以看到数据按预期放入 Redux 存储中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.