尝试在我的reducer中设置状态时,我遇到了一个奇怪的问题。
我有以下内容:
state = {
...state,
activeChannel: action.payload.channelList[0].name
.replace("&", "and")
.replace("`", "")
.replace("'", "")
.replace("-", "")
.replace(/\s/g, "")
.toLowerCase(),
preferedChannelsList: channelObjectTemp
};
当我在console.log上显示状态时,我可以看到preferredChannelsList设置正确,但其中的activeChannel不存在。
当我删除... state时,我可以看到activeChannel设置为state。 我使用传播运算符的方式有什么问题吗?
更新:reducer中的更多代码
switch (action.type) {
case GET_CHANNEL:
let totalNumberOfStories=0;
let activeChannelList=action.payload.channelList.slice(0,lazyLoadingStep);
let channelObjectTemp= {
...state.preferedChannelsList,
[action.payload.channelList[0].name.replace("&","and").replace("`","").replace("'","").replace('-','').replace(/\s/g, '').toLowerCase()]:action.payload.channelList[0]};
state={...state,activeChannel: action.payload.channelList[0].name.replace("&","and").replace("`","").replace("'","").replace('-','').replace(/\s/g, '').toLowerCase(),
"preferedChannelsList":channelObjectTemp };
break;