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 { Country, Photon } from '@prisma/photon'; | |
| import { findManyCursor } from './findManyCursor'; | |
| const photon = new Photon(); | |
| let data: Country[]; | |
| const createCountry = async (id: string) => photon.countries.create({ | |
| data: { | |
| id, |
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
| const store = createStore<StoreModel>({ | |
| todos: { | |
| // ... | |
| add: (state, payload) => { | |
| return { | |
| ...state, | |
| items: [ | |
| ...state.items, | |
| payload | |
| ] |
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 { 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) |
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 { render } from "react-dom"; | |
| import { StoreProvider } from "easy-peasy"; // 👈 | |
| import store from "./store"; | |
| render( | |
| <StoreProvider store={store}> | |
| <App /> | |
| </StoreProvider>, | |
| document.querySelector("#root"); |
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
| export default function Todos() { | |
| const items = useStore(state => state.todos.items); | |
| // 👇 | |
| const save = useActions(actions => actions.todos.save); | |
| const [newTodo, setNewTodo] = useState(""); | |
| return ( | |
| <div> |
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 { thunk } from "easy-peasy"; // 👈 | |
| const store = createStore<StoreModel>({ | |
| todos: { | |
| items: ["Install easy-peasy", "Build app", "profit"], | |
| // 👇renamed | |
| saved: (state, payload) => { | |
| state.items.push(payload); | |
| }, | |
| // 👇our thunk |
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 { Action, Thunk } from 'easy-peasy'; | |
| type TodosModel = { | |
| items: string[]; | |
| saved: Action<TodosModel, string>; | |
| save: Thunk<TodosModel, string>; // 👈 | |
| } |
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 { 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); |
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 { createTypedHooks } from "easy-peasy"; | |
| import { StoreModel } from "./model"; | |
| const { | |
| useActions, | |
| useStore, | |
| useDispatch | |
| } = createTypedHooks<StoreModel>(); | |
| export { useActions, useDispatch, useStore }; |
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
| store.dispatch.todos.add("Finish reading this article"); |
NewerOlder