reactjs - 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} />;
})}
解决方案
推荐阅读
- sql - 如何替换 Oracle 数据库中 nclob 类型列中的 xml 元素?
- r - 如何删除列表中的相同行?
- apple-music - 历史:播放的歌曲
- pic - PIC如何将PPS映射到正常输入
- nuget - 如何使用 JFrog CLI 按版本号对 Artifactory 包搜索结果进行排序?
- sql - 如何更改此动态查询
- php - tcpdf 中的 SVG 渲染器不支持 00FF 以上的 UTF-8 字符吗?
- javascript - 将多个动作传递给 bindActionCreators
- r - override.aes 与列表参数的命名向量
- css - 如何应用 CSS 覆盖来阻止相对日期