-
-
Save MikeBild/a2ccd9b03a7f95896391f4d391624eff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import { render } from 'react-dom'; | |
const todosStore = { | |
todos: { | |
'1': { | |
id: '1', | |
text: '', | |
}, | |
}, | |
editTodo(setTodosList, { id, text }) { | |
this.todos[id] = { ...this.todos[id], text }; | |
setTodosList(Object.values(this.todos)); | |
}, | |
}; | |
const App = () => { | |
const [todosList, setTodosList] = useState(Object.values(todosStore.todos)); | |
const editTodoList = todo => todosStore.editTodo(setTodosList, todo); | |
return ( | |
<> | |
<DisplayTodoText todo={todosStore.todos[1]} /> | |
<TodoList | |
todos={todosList} | |
renderTodo={({ id, text }) => ( | |
<input | |
defaultValue={text} | |
placeholder="Enter a text" | |
onChange={e => editTodoList({ id, text: e.target.value })} | |
/> | |
)} | |
/> | |
</> | |
); | |
}; | |
render(<App />, document.getElementById('root')); | |
function DisplayTodoText({ todo: { text } }) { | |
return <h1>{text}</h1>; | |
} | |
function TodoList({ todos = [], renderTodo = () => {} }) { | |
return ( | |
<ul> | |
{todos.map(({ id, text }) => ( | |
<li key={id}>{renderTodo({ id, text })}</li> | |
))} | |
</ul> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment