我正在使用REDX做待辦事項列表,並且我想爲每個待辦事項添加子待辦事項列表,但我無法理解爲什麼我的縮減器行無法正常工作(請參閱上述內容) 您能請幫幫我 ?Redux待辦事項列表高級
import { combineReducers } from 'redux'
import { ADD_TODO, TOGGLE_TODO, ADD_SUB_TODO} from '../constants/ActionTypes'
import _ from 'lodash'
const initialState = {
todos : []
}
export function todo(state, action) {
switch (action.type) {
case ADD_TODO:
return {
id: action.id,
text: action.text,
completed: false
};
default:
return state;
}
}
export function allTodo (state = initialState, action) {
switch (action.type) {
case ADD_TODO:
return {
...state,
todos: [
...state.todos,
{
id: action.id,
text: action.text,
completed: false,
subtodo:[]
}
]
};
case ADD_SUB_TODO:
console.log("REDUCER")
console.log(...state.todos)
return {
...state,
// THIS LINE DOES'NT WORK :
...state.todos[0].subtodo: [ ...state.todos[0].subtodo, {
id: action.id,
text: action.text
}]
};
default:
return state;
}
};
export default combineReducers({
allTodo
})
此行不工作:
...state.todos[0].subtodo: [ ...state.todos[0].subtodo, {
id: action.id,
text: action.text
}]
,這是我的子ToDo對象:
{
id: action.id,
text: action.text
}
怎麼辦你期望'state.todos [0] .subtodo'永遠是subtodo的正確位置嗎?是否只能在第一個待辦事項中添加一個子服務? – naomik
我爲我的測試放了一個0我會在將要解決此問題之後放置父級待辦事項的ID,當創建待辦事項時,創建子對話框時會出現子對話框的丟失情況ADD_TODO – fandro