-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ToDoのstate管理方法の変更 #17
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,23 @@ | ||
import { Container } from '@chakra-ui/react'; | ||
import React, { useState } from 'react'; | ||
|
||
import TodoInput from './conponents/TodoInput'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 素敵なコードですね! |
||
import TodoList from './conponents/TodoList'; | ||
|
||
type TodoType = { | ||
title: string; | ||
}; | ||
import TodoInput from '@/conponents/TodoInput'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 素敵です! |
||
import TodoList from '@/conponents/TodoList'; | ||
import { TodoType } from '@/types/TodoType'; | ||
|
||
const initialTodos: TodoType[] = [ | ||
{ title: '資料を作る' }, | ||
{ title: '歯医者に行く' }, | ||
{ title: '課題をやる' }, | ||
{ id: '0', title: '資料を作る', isCompleted: false }, | ||
{ id: '1', title: '歯医者に行く', isCompleted: false }, | ||
{ id: '2', title: '課題をやる', isCompleted: false }, | ||
]; | ||
|
||
const App = () => { | ||
const [todos, setTodos] = useState<TodoType[]>(initialTodos); | ||
const [status, setStatus] = useState<boolean[]>([false, false, false]); | ||
|
||
return ( | ||
<Container> | ||
<TodoInput todos={todos} setTodos={setTodos} status={status} setStatus={setStatus} /> | ||
<TodoList todos={todos} setTodos={setTodos} status={status} setStatus={setStatus} /> | ||
<TodoInput todos={todos} setTodos={setTodos} /> | ||
<TodoList todos={todos} setTodos={setTodos} /> | ||
</Container> | ||
); | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,36 +2,32 @@ import { CloseIcon } from '@chakra-ui/icons'; | |
import { Flex, Spacer, Editable, EditablePreview, EditableInput, Checkbox } from '@chakra-ui/react'; | ||
import React, { Dispatch, SetStateAction } from 'react'; | ||
|
||
type TodoType = { | ||
title: string; | ||
}; | ||
import { TodoType } from '@/types/TodoType'; | ||
|
||
type Props = { | ||
index: number; | ||
title: string; | ||
todos: TodoType[]; | ||
setTodos: Dispatch<SetStateAction<TodoType[]>>; | ||
status: boolean[]; | ||
setStatus: Dispatch<SetStateAction<boolean[]>>; | ||
}; | ||
|
||
const Todo = ({ index, title, todos, setTodos, status, setStatus }: Props) => { | ||
const Todo = ({ index, title, todos, setTodos }: Props) => { | ||
const removeTodo = () => { | ||
const newTodos = todos.filter((_, i) => i !== index); | ||
setTodos(newTodos); | ||
}; | ||
|
||
const handleStatus = () => { | ||
const newStatus = [...status]; | ||
newStatus[index] = !newStatus[index]; | ||
setStatus(newStatus); | ||
const newStatus = [...todos]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const newStatus = todos; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 自分も原因分からないです...。 const handleStatus = () => {
const newStatus = todos.map((todo: TodoType, i: number): TodoType => {
return i === index
? { id: todo.id, title: todo.title, isCompleted: !todo.isCompleted }
: todo;
});
setTodos(newStatus);
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ちょっと調べた感じだと、一般的には [Qiita] ReactのsetStateで配列の一部を変更する There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. じゃあやっぱここはコピーが必要なのかな。 |
||
newStatus[index].isCompleted = !newStatus[index].isCompleted; | ||
setTodos(newStatus); | ||
}; | ||
|
||
return ( | ||
<Flex align="center" w="90%" m="0 auto"> | ||
<Checkbox mr={2} onChange={() => handleStatus()} /> | ||
{<Checkbox mr={2} onChange={() => handleStatus()} />} | ||
<Editable defaultValue={title}> | ||
{status[index] ? <EditablePreview as="s" /> : <EditablePreview />} | ||
{todos[index].isCompleted ? <EditablePreview as="s" /> : <EditablePreview />} | ||
<EditableInput /> | ||
</Editable> | ||
<Spacer /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
import { Input } from '@chakra-ui/react'; | ||
import React, { useState, ChangeEvent, Dispatch, SetStateAction, KeyboardEvent } from 'react'; | ||
|
||
type TodoType = { | ||
title: string; | ||
}; | ||
import { TodoType } from '@/types/TodoType'; | ||
|
||
type Props = { | ||
todos: TodoType[]; | ||
setTodos: Dispatch<SetStateAction<TodoType[]>>; | ||
status: boolean[]; | ||
setStatus: Dispatch<SetStateAction<boolean[]>>; | ||
}; | ||
|
||
const TodoInput = ({ todos, setTodos, status, setStatus }: Props) => { | ||
const TodoInput = ({ todos, setTodos }: Props) => { | ||
const [title, setTitle] = useState<string>(''); | ||
|
||
return ( | ||
|
@@ -24,10 +20,11 @@ const TodoInput = ({ todos, setTodos, status, setStatus }: Props) => { | |
onChange={(e: ChangeEvent<HTMLInputElement>) => setTitle(e.target.value)} | ||
onKeyPress={(e: KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === 'Enter') { | ||
const newTodos = [...todos, { title: title }]; | ||
const newStatus = [...status, false]; | ||
const newTodos = [ | ||
...todos, | ||
{ id: String(todos.length), title: title, isCompleted: false }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これ私が指示して書いてもらったんだけど、全然考慮できてなかった 🙄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 今回は「タスク追加ごとにひたすらインクリメントのみしてく値を用意する」というもので解消しました。 |
||
]; | ||
setTodos(newTodos); | ||
setStatus(newStatus); | ||
} | ||
}} | ||
/> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type TodoType = { | ||
id: string; | ||
title: string; | ||
isCompleted: boolean; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
良いコードですね!