|
| 1 | +# tinystate - Tiny React State Management Library |
| 2 | + |
| 3 | +**tinystate** is a super small state management solution for React applications. |
| 4 | +It provides a simple and efficient way to manage and share state across your |
| 5 | +components without the complexity of larger state management libraries. With |
| 6 | +just a few lines of code, you can integrate **tinystate** into your React |
| 7 | +project and start managing your application's state effortlessly. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +You can install **tinystate** using npm or yarn: |
| 12 | + |
| 13 | +```bash |
| 14 | +npm install tinystate |
| 15 | +# or |
| 16 | +yarn add tinystate |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Creating a Store |
| 22 | + |
| 23 | +To create a store, use the `createStore` function from the **tinystate** |
| 24 | +library. You'll need to provide an initial state for your store: |
| 25 | + |
| 26 | +```javascript |
| 27 | +import { createStore } from "tinystate"; |
| 28 | + |
| 29 | +const countStore = createStore({ initialState: 0 }); |
| 30 | +``` |
| 31 | + |
| 32 | +### Using the Store |
| 33 | + |
| 34 | +After creating a store, you can create a custom hook to access and manage the |
| 35 | +state within your React components: |
| 36 | + |
| 37 | +```javascript |
| 38 | +import { createUseStore } from "tinystate"; |
| 39 | + |
| 40 | +export const useCountStore = createUseStore(countStore); |
| 41 | +``` |
| 42 | + |
| 43 | +Now, you can use the `useCountStore` hook in your components to access and |
| 44 | +update the state: |
| 45 | + |
| 46 | +```javascript |
| 47 | +import { useCountStore } from "./path-to-your-store"; |
| 48 | + |
| 49 | +function Counter() { |
| 50 | + const [count, setCount] = useCountStore(); |
| 51 | + |
| 52 | + const increment = () => { |
| 53 | + setCount(count + 1); |
| 54 | + }; |
| 55 | + |
| 56 | + const decrement = () => { |
| 57 | + setCount(count - 1); |
| 58 | + }; |
| 59 | + |
| 60 | + return ( |
| 61 | + <div> |
| 62 | + <p>Count: {count}</p> |
| 63 | + <button onClick={increment}>Increment</button> |
| 64 | + <button onClick={decrement}>Decrement</button> |
| 65 | + </div> |
| 66 | + ); |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### API Reference |
| 71 | + |
| 72 | +#### `createStore(options: CreateStoreOptions)` |
| 73 | + |
| 74 | +Creates a store instance with the provided initial state. |
| 75 | + |
| 76 | +- `options` (object): |
| 77 | + - `initialState` (any): The initial state for the store. |
| 78 | + |
| 79 | +Returns a store object with the following methods: |
| 80 | + |
| 81 | +- `subscribe(fn: Listener): () => void`: Subscribes a listener function to state |
| 82 | + changes and returns an unsubscribe function. |
| 83 | +- `getSnapshot(): T`: Returns the current snapshot of the state. |
| 84 | +- `setState(newState: T): void`: Updates the state and notifies subscribers of |
| 85 | + the change. |
| 86 | + |
| 87 | +#### `createUseStore(store: Store)` |
| 88 | + |
| 89 | +Creates a custom hook that encapsulates the store's subscription and state |
| 90 | +management. |
| 91 | + |
| 92 | +- `store` (Store): The store instance created using `createStore`. |
| 93 | + |
| 94 | +Returns a function that, when called, returns a tuple containing: |
| 95 | + |
| 96 | +- A getter function for the current state snapshot. |
| 97 | +- A setter function to update the state. |
| 98 | + |
| 99 | +## License |
| 100 | + |
| 101 | +This project is licensed under the [MIT License](LICENSE). |
0 commit comments