[英]Change inside key of a state in redux reducer
I have following reducer: 我有以下减速器:
const ListingReducer = (state={
fetched:false
},action) => {
if ( action.payload )
{
console.log("action.token",action.token);
console.log("action.payload.profiles",action.payload.profiles);
}
switch(action.type)
{
case 'SET_LISTING_DATA':
state = {
...state,
[action.token] : action.payload,
fetched : true
}
break;
case 'APPEND_LISTING_DATA':
// console.log("Previous state was: "state[action.token]);
state[action.token]
= {
...state[action.token],
profiles : [...state[action.token].profiles,...action.payload.profiles],
fetched : true
}
// console.log("Next state is: "+state[action.token]);
break;
}
return state;
}
The action: 那个行动:
action.token
, which have a key called profiles
. action.token
,该键具有名为profiles
的键。 profile
key of state. profile
密钥中。 I can see the added profiles in profile key, but it doesn't update the view. 我可以在配置文件密钥中看到添加的配置文件,但是它不会更新视图。
The profile is located in: 该配置文件位于:
state[action.token] ={name:y,data:xy,profiles:[id:x,{},{}],...}
It looks like you need to change state[action.token]
but also keep previous state data otherwise you will always override your state. 看起来您需要更改
state[action.token]
但还需要保留以前的状态数据,否则您将始终覆盖状态。
Changing/mutating state props directly is not a good practice though. 但是,直接更改/更改状态道具不是一个好习惯。
case 'APPEND_LISTING_DATA':
return {
...state,
[action.token]: {
...state[action.token],
profiles: [
...state[action.token].profiles,
...action.payload.profiles,
],
fetched: true,
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.