React初心者です
mapメソッドが定義されていないとのエラーなのですが、
taskが渡されていて、なぜマップメソッドが反応しないのかがわかりません、、、
TaskList.js import React from 'react' import {useSelector} from 'react-redux' import {selectTasks} from './taskSlice' import TaskItem from './TaskItem' const TaskList = () => { const tasks = useSelector(selectTasks) return ( <div> {tasks.map((task) => ( <TaskItem key={task.id} task={task}/> ))} </div> ) } export default TaskList
TaskItem.js import React from 'react' import {useDispatch} from 'react-redux' import {completeTask, deleteTask} from "./taskSlice" const TaskItem = ({task}) => { const dispatch = useDispatch() return ( <div> <input type="checkbox" onClick = {() =>dispatch(completeTask(task))} defaultChecked={task.completed} /> <span>{task.title}</span> <button onClick = {() =>dispatch(deleteTask(task))}>delete</button> </div> ) } export default TaskItem
taskSlice.js import { createSlice } from '@reduxjs/toolkit'; export const taskSlice = createSlice({ name: 'task', initialState: { idCounter:{ idCount: 3, }, tasks: [ {id: 1, title: 'TASK A',completed: false}, {id: 2, title: 'TASK C',completed: false}, {id: 3, title: 'TASK C',completed: false}, ] }, reducers: { newTask: (state, action) => { state.idCount ++; const newItem ={ id: state.idCount, title: action.payload, completed: false }; state.tasks = [newItem ,...state.tasks]; }, completeTask: (state,action) => { const task = state.tasks.find((t) => t.di === action.payload.id) if(task){ task.completed = !true.completed; } }, deleteTask: (state,action) => { state.tasks += state.tasks.filter((t) =>t.id !== action.payload.id); }, }, }); export const { newTask, completeTask, deleteTask } = taskSlice.actions; export const selectTasks = (state) => state.task.tasks; export default taskSlice.reducer;
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/29 03:23
2020/08/29 04:36
2020/08/29 04:46
2020/08/29 05:33
2020/08/29 06:02
2020/08/29 06:13