Last active
May 8, 2022 04:01
-
-
Save Kelin2025/15c78f0f63ae1a6b3db4c53eafeab534 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 from 'react' | |
import { useStore } from 'effector-react' | |
import { $todo, $todos, submitPressed, inputChanged, todoToggled, todoRemoved } from './stores' | |
export const TodoInput = () => { | |
// Use useStore(store) hook to get the state | |
// And update the component on state changes | |
const todo = useStore($todo) | |
return ( | |
<input value={todo} onChange={evt => inputChanged(evt.target.value)} /> | |
) | |
} | |
export const TodoForm = () => { | |
return ( | |
<form onSubmit={submitPressed}> | |
<TodoInput /> | |
<button>Add</button> | |
</form> | |
) | |
} | |
export const TodoList = () => { | |
const todos = useStore($todos) | |
return ( | |
<div> | |
{todos.map((todo, idx) => ( | |
<div key={idx}> | |
<input type="checkbox" value={todo.completed} onChange={() => todoToggled(idx)} /> | |
<span>{todo}</span> | |
<button onClick={() => todoRemoved(idx)}>X</button> | |
</div> | |
))} | |
</div> | |
) | |
} | |
export const TodoApp = () => { | |
return ( | |
<div> | |
<TodoForm /> | |
<TodoList /> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment