首页 > 解决方案 > React - 尝试添加功能错误 = TypeError: state.notes.map is not a function

问题描述

我正在尝试修补这个待办事项列表应用程序,并且 add 函数抛出错误,谁能解释为什么它会抛出这个错误?

TypeError:state.notes.map 不是函数

应用程序.js 文件

function reducer(state, action) {
    switch (action.type) {
        case ACTIONS.ADD_TODO:
            const newTodo = {
                id: Date.now(),
                name: action.payload.name,
                complete: false
            };
            return { ...state, notes: newTodo };
        default:
            return state;
    }
}

export default function App() {
    const [state, dispatch] = useReducer(reducer, {
        currentNote: null,
        notes: [
            { id: 1, name: 'Eat', complete: true },
            { id: 2, name: 'Eat More', complete: false },
            { id: 3, name: 'Eat Some More', complete: true }
        ]
    });

    const [name, setName] = useState('u');


   

这是导致错误的地方

            {state.notes.map(todo => {
                return <Todo key={todo.id} todo={todo} dispatch={dispatch} />;
            })}

标签: reactjstypeerror

解决方案


推荐阅读