Skip to content

Commit e2b80b0

Browse files
committed
fix: 배포 버그 수정
1 parent c03f81a commit e2b80b0

File tree

5 files changed

+125
-21
lines changed

5 files changed

+125
-21
lines changed

README.md

Lines changed: 118 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,127 @@
1-
# React + TypeScript + Vite
1+
# external-state
2+
external-state is an easy and lightweight React state management library.
23

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)
45

5-
Currently, two official plugins are available:
6+
### Installation
67

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);
938

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+
}
1148

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+
```
1373

14-
- Configure the top-level `parserOptions` property like this:
74+
4. Using state management hook: useExternalValue
1575

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;
2393
```
2494

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+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "external-state",
3-
"version": "0.1.18",
3+
"version": "0.1.20",
44
"private": false,
55
"description": "Easy and Lightweight State Management for React",
66
"type": "module",

src/lib/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { useExternalState } from "./external-state/tools.ts";
2+
export {useSetExternalState} from "./external-state/tools.ts";
3+
export {useExternalValue} from "./external-state/tools.ts";
4+
export {store} from "./external-state/tools.ts";

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
"noUnusedParameters": false,
2121
"noFallthroughCasesInSwitch": true
2222
},
23-
"include": ["src", "src/lib"],
23+
"include": ["src/lib"],
2424
"references": [{ "path": "./tsconfig.node.json" }]
2525
}

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default defineConfig({
99
base: "/external-state/",
1010
build: {
1111
lib: {
12-
entry: path.resolve(__dirname, "src/lib/external-state/index.ts"),
12+
entry: path.resolve(__dirname, "src/lib/index.tsx"),
1313
name: "index",
1414
fileName: "index",
1515
},

0 commit comments

Comments
 (0)