Skip to content

Instantly share code, notes, and snippets.

@ctrlplusb
Created February 8, 2019 08:01
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/42ae58df0952c106732ec9f4fb24ca6f to your computer and use it in GitHub Desktop.
Save ctrlplusb/42ae58df0952c106732ec9f4fb24ca6f to your computer and use it in GitHub Desktop.
import { StoreProvider, createStore, useStore, useActions } from 'easy-peasy';
// 👇 create your store, providing the model
const store = createStore({
todos: {
items: ['Install easy-peasy', 'Build app', 'Profit'],
// 👇 define actions directly on your model
add: (state, payload) => {
// do simple mutation to update state, and we make it an immutable update
state.items.push(payload)
// (you can also return a new immutable instance if you prefer)
}
}
});
const App = () => (
// 👇 wrap your app to expose the store
<StoreProvider store={store}>
<TodoList />
</StoreProvider>
)
function TodoList() {
// 👇 use hooks to get state or actions
const todos = useStore(state => state.todos.items)
const add = useActions(actions => actions.todos.add)
return (
<div>
{todos.map((todo, idx) => <div key={idx}>{todo}</div>)}
<AddTodo onAdd={add} />
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment