Skip to content

Instantly share code, notes, and snippets.

@ctrlplusb
Created February 8, 2019 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctrlplusb/ac59843d2a0fe9b74242178392cf910c to your computer and use it in GitHub Desktop.
Save ctrlplusb/ac59843d2a0fe9b74242178392cf910c to your computer and use it in GitHub Desktop.
import { useState } from "react";
import { useStore, useActions } from "./hooks"; // 👈
export default function Todos() {
// 👇 pull out state from our store
const items = useStore(state => state.todos.items);
// 👇 pull out actions from our store
const add = useActions(actions => actions.todos.add);
const [newTodo, setNewTodo] = useState("");
return (
<div>
<ul>
{items.map((todo, idx) =>
<li key={idx}>{todo}</li>
)}
</ul>
<input
type="text"
onChange={e => setNewTodo(e.target.value)}
value={newTodo}
/>
<button onClick={() => add(newTodo)}>Add</button>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment