๐ป Zustand Copilot ์๊ฐ: Zustand ์ํ ๊ด๋ฆฌ๋ฅผ ์ํ ๊ถ๊ทน์ ์ธ VS Code ํ์ฅ ํ๋ก๊ทธ๋จ
Source: Dev.to

React์์ ์ํ ๊ด๋ฆฌ๋ฅผ ์ํด Zustand๋ฅผ ์ฌ์ฉํด ์๋ค๋ฉด, ๊ทธ ์ฐ์ํจ๊ณผ ๊ฐ๋ ฅํจ์ ์ด๋ฏธ ์๊ณ ์์ ๊ฒ๋๋ค. ํ์ง๋ง Zustand ๊ฐ๋ฐ ๊ฒฝํ์ ๋์ฑ ํฅ์์ํฌ ์ ์๋ค๋ฉด ์ด๋จ๊น์?
์ค๋, ์ ๋ Zustand Copilot์ ์๊ฐํ๊ฒ ๋์ด ๊ธฐ์ฉ๋๋ค โ Zustandโฏv5+ ๊ฐ๋ฐ์ ์ํ ๊ถ๊ทน์ ์ธ ํดํท์ด ๋๋๋ก ์ค๊ณ๋ VSโฏCode ํ์ฅ ํ๋ก๊ทธ๋จ์ ๋๋ค.
๐ค ๋ด๊ฐ ์ด๊ฒ์ ๋ง๋ ์ด์
- ๐ ๊ฐ์ ์คํ ์ด ๋ณด์ผ๋ฌํ๋ ์ดํธ๋ฅผ ๋ฐ๋ณตํด์ ํ์ดํํ๊ธฐ
- ๐ ๊ตฌ๋ฌธ ์ฐธ๊ณ ๋ฅผ ์ํด ๋ฌธ์๋ฅผ ๊ณ์ ์ ํํ๊ธฐ
- ๐ ๋ค์ค ๊ฐ ์
๋ ํฐ์
useShallow์ฌ์ฉ์ ์์ (๋ถํ์ํ ์ฌ๋ ๋๋ง ๋ฐ์!) - ๐ ํฐ ์ฑ์ ์ํด Slices Pattern์ ์๋์ผ๋ก ์ค์ ํ๊ธฐ
Zustand Copilot์ ์ด๋ฌํ ๋ชจ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํฉ๋๋ค.
โจ ์ฃผ์ ๊ธฐ๋ฅ
1. TypeScriptโFirst ์ค๋ํซ
ํ๋ฆฌํฝ์ค๋ฅผ ์ ๋ ฅํ๋ฉด ๋ฐ๋ก ํ๋ก๋์ ์์ค์ ์ฝ๋๋ฅผ ์ป์ ์ ์์ต๋๋ค.
| ์ ๋์ฌ | ์ป๋ ๋ด์ฉ |
|---|---|
zstore | ๊ฐ๋ฐ์ ๋๊ตฌ๊ฐ ํฌํจ๋ ์์ ํ ์คํ ์ด |
zslice | Slices ํจํด์ฉ ์ฌ๋ผ์ด์ค |
zpersist | localStorage์ ํจ๊ป ์๊ตฌํ๋ ์คํ ์ด |
zshallow | useShallow ์
๋ ํฐ (์ฑ๋ฅ ์ต์ ํ!) |
zasync | ๋ก๋ฉ ์ํ๋ฅผ ํฌํจํ ๋น๋๊ธฐ ์คํ ์ด |
zimmer | Immerโ๊ธฐ๋ฐ ๊ฐ๋ณ ์ ๋ฐ์ดํธ |
์์ โ zstore๋ฅผ ์
๋ ฅํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋์ต๋๋ค:
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
interface CounterState {
count: number;
increment: () => void;
decrement: () => void;
reset: () => void;
}
export const useCounterStore = create()(
devtools(
(set) => ({
count: 0,
increment: () =>
set((state) => ({ count: state.count + 1 }), false, 'increment'),
decrement: () =>
set((state) => ({ count: state.count - 1 }), false, 'decrement'),
reset: () => set({ count: 0 }, false, 'reset'),
}),
{ name: 'CounterStore' }
)
);
2. ์ค๋งํธ ์ฝ๋ ์ก์
์คํ ์ด๋ฅผ ๋ง์ฐ์ค ์ค๋ฅธ์ชฝ ๋ฒํผ์ผ๋ก ํด๋ฆญํ๋ฉด ์ง๋ฅํ ๋ฆฌํฉํฐ๋ง ์ต์ ์ ์ ๊ณตํฉ๋๋ค:
- devtools๋ก ๋ํ โ Redux DevTools ํตํฉ์ ์ฆ์ ์ถ๊ฐ
- persist๋ก ๋ํ โ
localStorage์๊ตฌํ ์ถ๊ฐ - useShallow ์ถ๊ฐ โ ์ ๋ ํฐ๋ฅผ ์ฑ๋ฅ ์ต์ ํ
- Slices ํจํด์ผ๋ก ์ถ์ถ โ ํฐ ์คํ ์ด๋ฅผ ๋ชจ๋ํ
3. ์ค์๊ฐ ํธ๋ฒ ๋ฌธ์ํ
Zustand ํจ์ ์์ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ฆฌ๋ฉด ๋ค์์ ํ์ธํ ์ ์์ต๋๋ค:
- ๐ ์์ธ ์ค๋ช
- ๐ TypeScript ์๊ทธ๋์ฒ
- ๐ป ์ฌ์ฉ ์์
- โก ์ฑ๋ฅ ํ
- ๐ ๊ณต์ ๋ฌธ์ ๋งํฌ
4. ์ง๋ฅํ ์๋ import
ํ์ดํ์ ์์ํ๋ฉด ๋ค์์ ๋ํ ์ค๋งํธ import ์ ์์ ๋ฐ์ต๋๋ค:
create,createStore,useStoredevtools,persist,immeruseShallow(์ฑ๋ฅ ํํธ ํฌํจ!)StateCreator,StoreApiํ์
5. ๋ช ๋ น ๊ธฐ๋ฐ ์คํ ์ด ์์ฑ
๋ช
๋ น ํ๋ ํธ(Ctrl+Shift+P)๋ฅผ ์ฌ์ฉํ์ฌ:
- ์ ์คํ ์ด ๋ง๋ค๊ธฐ โ ์ธํฐ๋ํฐ๋ธ ์คํ ์ด ์์ฑ๊ธฐ
- ์ ์ฌ๋ผ์ด์ค ๋ง๋ค๊ธฐ โ ์ฌ๋ผ์ด์ค ํ์ผ ์์ฑ
- Slices ํจํด ์คํ ์ด ์์ฑ โ ์ ์ฒด ํ๋ก์ ํธ ๊ตฌ์กฐ ์๋ ์์ฑ
โก Performance Best Practices BuiltโIn
Zustand Copilot์ ์ฝ๋๋ฅผ ๋ ๋น ๋ฅด๊ฒ ์์ฑํ๋๋ก ๋์์ค ๋ฟ๋ง ์๋๋ผ ๋ ๋์ ์ฝ๋๋ฅผ ์์ฑํ๋๋ก ๋์์ค๋๋ค.
useShallow Everywhere
// โ Bad: Creates a new object on every render
const { name, age } = useStore((state) => ({
name: state.name,
age: state.age,
}));
// โ
Good: Shallow comparison prevents reโrenders
const { name, age } = useStore(
useShallow((state) => ({
name: state.name,
age: state.age,
}))
);
ํ์ฅ ํ๋ก๊ทธ๋จ์ useShallow๋ฅผ ๋นผ๋จน์์ ๋ ๊ฒฝ๊ณ ๋ฅผ ํ์ํ๊ณ ์๋์ผ๋ก ์ถ๊ฐํด ์ค ์ ์์ต๋๋ค!
Slices Pattern for Scale
๋๊ท๋ชจ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ํด ํ์ฅ ํ๋ก๊ทธ๋จ์ Slices Pattern์ ๊ถ์ฅํฉ๋๋ค:
src/stores/
โโโ index.ts # Combined store
โโโ slices/
โโโ authSlice.ts # Authentication
โโโ userSlice.ts # User data
โโโ settingsSlice.ts # Settings
Zustand: Generate Slices Pattern Store ๋ช
๋ น์ ์คํํ๋ฉด ์ด ๊ตฌ์กฐ๋ฅผ ์ฆ์ ์ค์บํด๋ฉํ ์ ์์ต๋๋ค.
๐ ์์ํ๊ธฐ
์ค์น
- VSโฏCode๋ฅผ ์ฝ๋๋ค.
Ctrl+Shift+X๋ฅผ ๋๋ฌ Extensions๋ฅผ ์ฝ๋๋ค.- **โZustand Copilotโ**์ ๊ฒ์ํฉ๋๋ค.
- Install๋ฅผ ํด๋ฆญํฉ๋๋ค.
๋๋ CLI๋ฅผ ํตํด ์ค์น:
code --install-extension devplusfun.zustand-copilot
๋น ๋ฅธ ์์
- ์๋ก์ด
.ts๋๋.tsxํ์ผ์ ๋ง๋ญ๋๋ค. zstore๋ฅผ ์ ๋ ฅํ๊ณ Tab ํค๋ฅผ ๋๋ฆ ๋๋ค.- ์๋ฆฌ ํ์์๋ฅผ ์ฑ์๋๋ค.
- ๋น๋๋ฅผ ์์ํฉ๋๋ค! ๐ป
๐ ์ฌ์ฉ ๊ฐ๋ฅํ ์ค๋ํซ
| Snippet | Description |
|---|---|
zstore | ๊ฐ๋ฐ์ ๋๊ตฌ๊ฐ ํฌํจ๋ ๊ธฐ๋ณธ ์คํ ์ด |
zslice | Slices ํจํด์ฉ ์ฌ๋ผ์ด์ค |
zpersist | ์์ํ๋ ์คํ ์ด |
zasync | ๋ก๋ฉ์ด ์๋ ๋น๋๊ธฐ ์คํ ์ด |
zimmer | Immer ๋ฏธ๋ค์จ์ด ์คํ ์ด |
zslices | ๊ฒฐํฉ๋ ์ฌ๋ผ์ด์ค ์คํ ์ด |
zshallow | useShallow ์
๋ ํฐ |
zselector | ๋ฉ๋ชจ์ด์ ์ด์ ๋ ์ ๋ ํฐ |
zsubscribe | ์คํ ์ด ๊ตฌ๋ |
zgetstate | React ์ธ๋ถ์์ ์ํ ์ ๊ทผ |
zcontext | ์ปจํ ์คํธ ํจํด ์คํ ์ด |
zcomputed | ๊ณ์ฐ๋ ์์ฑ |
zreset | ๋ฆฌ์ ๊ธฐ๋ฅ์ด ์๋ ์คํ ์ด |
zmiddleware | ์ ์ฒด ๋ฏธ๋ค์จ์ด ์คํ |
zactions | ๋ณ๋ ์ก์ ์ธํฐํ์ด์ค |
ztemporal | zundo๋ฅผ ์ด์ฉํ ์คํ ์ทจ์/์ฌ์คํ |
๐ ๊ตฌ์ฑ
VSโฏCode ์ค์ ์์ ํ์ฅ์ ์ฌ์ฉ์ ์ ์ํ์ธ์:
{
"zustandCopilot.autoImport.enabled": true,
"zustandCopilot.hoverDocs.enabled": true,
"zustandCopilot.codeActions.enabled": true,
"zustandCopilot.defaultStorePath": "src/stores",
"zustandCopilot.useShallowByDefault": true,
"zustandCopilot.snippets.enabled": true,
"zustandCopilot.commandPalette.enabled": true
}
๐ Feedback Welcome!
This is just version 1.0.0, and I have big plans for future releases:
- ๐ Store visualization panel
- ๐งช Testing utilities
- ๐ Redux โ Zustand migration tools
- ๐ Performance analysis
What features would you like to see? Drop a comment below or open an issue on GitHub!
๐ ๋งํฌ
- ๐ฆ VS Code Marketplace
- ๐ Zustand Official Docs
If this extension helps your Zustand development, please โญ star the repo and leave a review on the marketplace! It really helps others discover it.
โ ์ด ํ์ฅ์ด ์ฌ๋ฌ๋ถ์ Zustand ๊ฐ๋ฐ์ ๋์์ด ๋๋ค๋ฉด, โญ ๋ ํฌ์ ๋ณํ๋ฅผ ๋ฌ๊ณ ๋ง์ผํ๋ ์ด์ค์ ๋ฆฌ๋ทฐ๋ฅผ ๋จ๊ฒจ ์ฃผ์ธ์! ๋ค๋ฅธ ์ฌ๋๋ค์ด ์ฐพ๋ ๋ฐ ํฐ ๋์์ด ๋ฉ๋๋ค.
Happy coding! ๐ปโจ
{
"ot.includeDevtools": true
} 