我有这样的情况......
const INITIAL_STATE = { 聊天:[] }
然后我设置了聊天,我包含了这些数据:
[
{
"otherParty":"aaaaa",
"thread":[
{
"a":1,
"b":2,
"c":3
},
{
"d":4,
"e":5,
"f":6
}
]
},
{
"otherParty":"bbbb",
"thread":[
{
"a":1,
"b":2,
"c":3
},
{
"d":4,
"e":5,
"f":6
}
]
},
{
"otherParty":"cccc",
"thread":[
{
"a":1,
"b":2,
"c":3
},
{
"d":4,
"e":5,
"f":6
}
]
}
]
我需要在数组[1]中添加一个新项目。例如{g:7,h:8,i:9} - 换句话说:我想指定数组的索引并添加一个新线程。
如何存档?
export const addNewThread = (obj, index) => {
return {
type: ADD_NEW_THREAD,
payload: {
thread: obj,
index: index
}
}
}
和减速机......(我需要填写????)
const INITIAL_STATE = {
chat: []
}
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case ADD_NEW_THREAD:
return {
...state,
chat: ?????
}
}
return state
}
答案 0 :(得分:1)
像这样的东西
const INITIAL_STATE = {
chat: []
}
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case ADD_NEW_THREAD:
const chat = state.chat.slice();
const thread = chat[action.index].thread.concat(action.thread);
chat.splice(action.index, 1, thread);
return {
chat
};
}
return state
}