atoms-state v0.8.1
git clone https://git.pyrossh.dev/atoms-state
Simple State management for react
README.md
| cdb7dd5 | 1 | # atoms-state |
| 6d09b46 | 2 | |
| 3e0308c | 3 | State management and common hooks |
| cd04708 | 4 | |
| cd04708 | 5 | ## Usage |
| c056c55 | 6 | |
| cd04708 | 7 | ```js |
| cd04708 | 8 | import React from 'react'; |
| cd04708 | 9 | import ReactDOM from 'react-dom'; |
| fcb6563 | 10 | import { atom, useAtom } from 'atoms-state'; |
| cd04708 | 11 | |
| cd04708 | 12 | const countAtom = atom(10); |
| 3e0308c | 13 | const sumAtom = atom((get) => get(countAtom) + 10); |
| cd04708 | 14 | |
| cd04708 | 15 | const increment = () => { |
| 3e0308c | 16 | countAtom.update((count) => count + 1); |
| cd04708 | 17 | }; |
| cd04708 | 18 | |
| cd04708 | 19 | const decrement = () => { |
| 3e0308c | 20 | countAtom.update((count) => count - 1); |
| cd04708 | 21 | }; |
| cd04708 | 22 | |
| cd04708 | 23 | const Counter = () => { |
| cd04708 | 24 | const count = useAtom(countAtom); |
| cd04708 | 25 | const sum = useAtom(sumAtom); |
| cd04708 | 26 | |
| cd04708 | 27 | return ( |
| cd04708 | 28 | <div> |
| cd04708 | 29 | <p>Count: {count}</p> |
| cd04708 | 30 | <p>Sum: {sum}</p> |
| cd04708 | 31 | <button onClick={increment}>Inc</button> |
| cd04708 | 32 | <button onClick={decrement}>Dec</button> |
| cd04708 | 33 | </div> |
| cd04708 | 34 | ); |
| cd04708 | 35 | }; |
| cd04708 | 36 | |
| cd04708 | 37 | ReactDOM.render(<Counter />, document.getElementById('root')); |
| cd04708 | 38 | ``` |
| c056c55 | 39 | |
| c056c55 | 40 | ## Async |
| c056c55 | 41 | |
| c056c55 | 42 | ```js |
| c056c55 | 43 | import React, { Suspense } from 'react'; |
| c056c55 | 44 | import ReactDOM from 'react-dom'; |
| fcb6563 | 45 | import { asyncAtom, useAsyncAtom } from 'atoms-state'; |
| c056c55 | 46 | |
| c056c55 | 47 | const todoAtom = asyncAtom(async ({ id }) => { |
| c056c55 | 48 | const res = await fetch(`https://jsonplaceholder.typicode.com/todos/${id}`); |
| c056c55 | 49 | return await res.json(); |
| c056c55 | 50 | }); |
| c056c55 | 51 | |
| c056c55 | 52 | const completeTodo = () => { |
| 3e0308c | 53 | todoAtom.update((todo) => ({ ...todo, completed: !todo.completed })); |
| c056c55 | 54 | }; |
| c056c55 | 55 | |
| c056c55 | 56 | const Counter = () => { |
| c056c55 | 57 | const todo = useAsyncAtom(todoAtom, { id: 1 }); |
| c056c55 | 58 | |
| c056c55 | 59 | return ( |
| c056c55 | 60 | <div> |
| c056c55 | 61 | <p>id: {todo.id}</p> |
| c056c55 | 62 | <p>userId: {todo.userId}</p> |
| c056c55 | 63 | <p>title: {todo.title}</p> |
| c056c55 | 64 | <p>completed: {todo.completed}</p> |
| c056c55 | 65 | <button onClick={completeTodo}>Toggle Complete</button> |
| c056c55 | 66 | </div> |
| c056c55 | 67 | ); |
| c056c55 | 68 | }; |
| c056c55 | 69 | |
| c056c55 | 70 | ReactDOM.render( |
| c056c55 | 71 | <Suspense fallback={<div>Loading</div>}> |
| c056c55 | 72 | <Counter /> |
| c056c55 | 73 | </Suspense>, |
| 3e0308c | 74 | document.getElementById('root'), |
| c056c55 | 75 | ); |
| c056c55 | 76 | ``` |