在 ReactJS 中改变对象的 Redux 存储数组
Mutating Redux Store Array of Objects in ReactJS
我的商店由一组对象组成:
const INIT_STATE = {
users:[],
contacts : []
};
我正在尝试像这样更改存储数组:
const App = (state = INIT_STATE, action) => {
switch (action.type) {
case BLOCK_CONTACT:
state.contacts.map((contact, index) => {
if (contact._id === action.payload) {
state.contacts[index].status = "blocked;
return {
...state
};
}
return {
...state
};
})
return {
...state
};
}
}
但我的店面价值没有改变。
当我在 if 语句之后记录状态值时,我得到了正确的状态值,但我的状态没有被更新。我认为这可能与我的 return
陈述有关,但在这一点上我尝试了不同的变体,但没有成功。
任何帮助将不胜感激。
此外,如果您的应用处于启动阶段,您可以阅读 this article about jotai state management (Jotai)
我明白了。万一其他人可能需要帮助,这里是代码...
const App = (state = INIT_STATE, action) => {
switch (action.type) {
case BLOCK_CONTACT:
var list = state.contacts.map(contact => {
var blockList = {
...contact
};
if (contact._id === action.payload) {
blockList.status = 1;
}
return blockList;
})
return {
...state,
contacts: list
};
}
}
我的商店由一组对象组成:
const INIT_STATE = {
users:[],
contacts : []
};
我正在尝试像这样更改存储数组:
const App = (state = INIT_STATE, action) => {
switch (action.type) {
case BLOCK_CONTACT:
state.contacts.map((contact, index) => {
if (contact._id === action.payload) {
state.contacts[index].status = "blocked;
return {
...state
};
}
return {
...state
};
})
return {
...state
};
}
}
但我的店面价值没有改变。
当我在 if 语句之后记录状态值时,我得到了正确的状态值,但我的状态没有被更新。我认为这可能与我的 return
陈述有关,但在这一点上我尝试了不同的变体,但没有成功。
任何帮助将不胜感激。
此外,如果您的应用处于启动阶段,您可以阅读 this article about jotai state management (Jotai)
我明白了。万一其他人可能需要帮助,这里是代码...
const App = (state = INIT_STATE, action) => {
switch (action.type) {
case BLOCK_CONTACT:
var list = state.contacts.map(contact => {
var blockList = {
...contact
};
if (contact._id === action.payload) {
blockList.status = 1;
}
return blockList;
})
return {
...state,
contacts: list
};
}
}