reactjs - 如何在 Redux 中正确清理和重写数组
问题描述
点击按钮我得到输入值和数组。
<button onClick={()=>this.props.test(this.props.value,this.props.pokemonList)}>Push</button>
在行动中,我映射它并将数组中的值“类型”与输入值相等,如果它们相等,则将它们置于状态
export function filterByTypes(searchValue,pokList){
return (dispatch) =>{
dispatch(getValue(searchValue));
pokList.map((types)=>
types.types.map((name)=>{
if (searchValue == name.type.name){
dispatch(filteredPokes(types));
}})
);
}
}
减速器
case FILTERED_POKES:
return{
...state, filteredPokes: [...state,action.types]
}
我需要每次点击数组filteredPokes 时清除并在那里放置新元素。我试着这样做......状态,filteredPokes:[action.types],但在这种情况下,数组中只放一个元素。我该如何修复它或进行正确的检查?谢谢!
解决方案
减速器
case FILTERED_POKES:
return {
...state,
filteredPokes: [...state.filretedPokes, ...action.types]
}
在您的情况下,您将所有内容复制state
到filteredPokes
. filteredPokes
通过单击创建新操作进行清除。