|
1 | | -# React + TypeScript + Vite |
| 1 | +# external-state |
| 2 | +external-state is an easy and lightweight React state management library. |
2 | 3 |
|
3 | | -This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. |
| 4 | +[한국어](https://github.com/gabrielyoon7/external-state/blob/main/docs/readme-kr.md) |
4 | 5 |
|
5 | | -Currently, two official plugins are available: |
| 6 | +### Installation |
6 | 7 |
|
7 | | -- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh |
8 | | -- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh |
| 8 | +``` |
| 9 | +npm install external-store |
| 10 | +``` |
| 11 | + |
| 12 | +or |
| 13 | + |
| 14 | +``` |
| 15 | +yarn add external-store |
| 16 | +``` |
| 17 | + |
| 18 | +### Usage |
| 19 | +1. Creating initial state value |
| 20 | +- The initial state value is created using the `store()` function. |
| 21 | + |
| 22 | +```tsx |
| 23 | +import { store } from "external-state"; |
| 24 | + |
| 25 | +export const countStore = store<number>(0); |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +2. Using state management hook: useExternalState |
| 30 | +- `useExternalState()` has a similar usage pattern to the react `useState()` hook. |
| 31 | +- It has the same usage pattern as recoil's useRecoilState. |
| 32 | + |
| 33 | +```tsx |
| 34 | +import { useExternalState } from "external-state"; |
| 35 | + |
| 36 | +function Count() { |
| 37 | + const [count, setCount] = useExternalState(countStore); |
9 | 38 |
|
10 | | -## Expanding the ESLint configuration |
| 39 | + return ( |
| 40 | + <div> |
| 41 | + <div>{count}</div> |
| 42 | + <button onClick={() => setCount(count + 1)}> |
| 43 | + increase |
| 44 | + </button> |
| 45 | + </div> |
| 46 | + ) |
| 47 | +} |
11 | 48 |
|
12 | | -If you are developing a production application, we recommend updating the configuration to enable type aware lint rules: |
| 49 | +export default Count; |
| 50 | +``` |
| 51 | + |
| 52 | +3. Using state management hook: useSetExternalState |
| 53 | +- `useSetExternalState()` is a function used to update the state. |
| 54 | +- It has the same usage pattern as recoil's useSetRecoilState. |
| 55 | + |
| 56 | +```tsx |
| 57 | +import { useSetExternalState } from "external-state"; |
| 58 | + |
| 59 | +function Count() { |
| 60 | + const setCount = useSetExternalState(countStore); |
| 61 | + |
| 62 | + return ( |
| 63 | + <div> |
| 64 | + <button onClick={() => setCount(count + 1)}> |
| 65 | + increase |
| 66 | + </button> |
| 67 | + </div> |
| 68 | + ) |
| 69 | +} |
| 70 | + |
| 71 | +export default Count; |
| 72 | +``` |
13 | 73 |
|
14 | | -- Configure the top-level `parserOptions` property like this: |
| 74 | +4. Using state management hook: useExternalValue |
15 | 75 |
|
16 | | -```js |
17 | | - parserOptions: { |
18 | | - ecmaVersion: 'latest', |
19 | | - sourceType: 'module', |
20 | | - project: ['./tsconfig.json', './tsconfig.node.json'], |
21 | | - tsconfigRootDir: __dirname, |
22 | | - }, |
| 76 | +- `useExternalValue()` is a function used to subscribe the state value. |
| 77 | +- It has the same usage pattern as recoil's useRecoilValue. |
| 78 | + |
| 79 | +```tsx |
| 80 | +import { useExternalValue } from "external-state"; |
| 81 | + |
| 82 | +function Count() { |
| 83 | + const count = useExternalValue(countStore); |
| 84 | + |
| 85 | + return ( |
| 86 | + <div> |
| 87 | + {count} |
| 88 | + </div> |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +export default Count; |
23 | 93 | ``` |
24 | 94 |
|
25 | | -- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked` |
26 | | -- Optionally add `plugin:@typescript-eslint/stylistic-type-checked` |
27 | | -- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list |
| 95 | +5. Accessing/Modifying the store outside of React |
| 96 | + |
| 97 | +- Once a store is created, you can use the `.getState()` and `.setState()` methods. |
| 98 | +- The `.getState()` method reads the latest state value at the time of invocation. |
| 99 | +- The `.setState()` method updates the state value. |
| 100 | +- All of this can be done directly in a vanilla environment, and even if the state is modified outside of React, all components subscribing to the store through the hook will be accurately re-rendered. |
| 101 | +- It is also possible to use `async/await`. |
| 102 | +- You can separate the logic for better reusability, as shown in the example below: |
| 103 | + |
| 104 | +```tsx |
| 105 | +export const countActions = { |
| 106 | + increase: () => { |
| 107 | + const prevCount = countStore.getState(); |
| 108 | + countStore.setState(prevCount + 1); |
| 109 | + }, |
| 110 | + decrease: () => { |
| 111 | + const prevCount = countStore.getState(); |
| 112 | + countStore.setState(prevCount - 1); |
| 113 | + }, |
| 114 | + increaseIfOdd: () => { |
| 115 | + const prevCount = countStore.getState(); |
| 116 | + if (prevCount % 2 === 1) { |
| 117 | + countStore.setState(prevCount + 1); |
| 118 | + } |
| 119 | + }, |
| 120 | + increaseAsync: async () => { |
| 121 | + const prevCount = countStore.getState(); |
| 122 | + const response = await fetchCount(1) |
| 123 | + const amount = response.data; |
| 124 | + countStore.setState(prevCount + amount) |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
0 commit comments