如何在Redux中正确地清理和重写数组



点击按钮,获取输入值和数组。

<button onClick={()=>this.props.test(this.props.value,this.props.pokemonList)}>Push</button>

在操作中,我映射它并使数组中的值"Types"与输入值相等,如果它们相等,则将它们置于状态

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清除并放入新元素。我试着这样做。。。state,filteredPokes:[action.types],但在这种情况下,数组中只放一个元素。我该如何修复或进行正确的检查?非常感谢。

还原剂

case FILTERED_POKES:
return {
...state, 
filteredPokes: [...state.filretedPokes, ...action.types]
}

在您的情况下,将所有state复制到filteredPokes。要清除filteredPokes,请单击"创建新操作"。

首先,您需要过滤数组,然后将数组置于状态

export function filterByTypes(searchValue,pokList){
return (dispatch) =>{
dispatch(getValue(searchValue));
pokList.forEach((types)=> {
dispatch(filteredPokes(types.types.filter((name)=> searchValue == name.type.name)));
}
}
}

最新更新