Zustand는 독일어로 '상태'라는 뜻을 가진 라이브러리이며 Jotai를 만든 카토 다이시가 제작에 참여하고 적극적으로 관리하는 라이브러리이다. 아래의 특징을 가지고 있다.
# NPM
npm install zustand
# Yarn
yarn add zustand
FirstChild
컴포넌트에서 store에 저장된 값을 변경시키고 SecondChild
컴포넌트에서 store에 저장된 값을 출력하도록 해보자.create
함수를 사용store.js
// store.js
import create from "zustand";
const useStore = create((set) => ({
count: 0,
selectedButton: null,
setSelectedButton: (button) => set({ selectedButton: button }),
incrementCount: () => set((state) => ({ count: state.count + 1 })),
removeCount: () => set({ count: 0 }),
}));
export default useStore;
useStore
함수를 호출FirstChild.jsx
// FirstChild.js
import React from "react";
import useStore from "../store/store.js";
function FirstChild() {
const setSelectedButton = useStore((state) => state.setSelectedButton);
const incrementCount = useStore((state) => state.incrementCount);
const removeCount = useStore((state) => state.removeCount);
const handleClick = (button) => {
setSelectedButton(button);
};
return (
<div>
<h1>FirstChild</h1>
<div>
<button onClick={() => handleClick("O")}>O</button>
<button onClick={() => handleClick("X")}>X</button>
</div>
<div>
<button onClick={incrementCount}>카운트 증가</button>
<button onClick={removeCount}>카운트 리셋</button>
</div>
</div>
);
}
export default FirstChild;
// FirstChild.js
import React from "react";
import useStore from "../store/store.js";
function FirstChild() {
const { setSelectedButton, incrementCount, removeCount } = useStore((state) => state);
const handleClick = (button) => {
setSelectedButton(button);
};
return (
<div>
<h1>FirstChild</h1>
<div>
<button onClick={() => handleClick("O")}>O</button>
<button onClick={() => handleClick("X")}>X</button>
</div>
<div>
<button onClick={incrementCount}>카운트 증가</button>
<button onClick={removeCount}>카운트 리셋</button>
</div>
</div>
);
}
export default FirstChild;
SecondChild.jsx
// SecondChild.js
import React from "react";
import useStore from "../store/store.js";
function SecondChild() {
const selectedButton = useStore((state) => state.selectedButton);
const count = useStore((state) => state.count);
return (
<div>
<h1>SecondChild</h1>
<p>카운트: {count}</p>
<p>선택한 버튼: {selectedButton}</p>
</div>
);
}
export default SecondChild;
// SecondChild.js
import React from "react";
import useStore from "../store/store.js";
function SecondChild() {
const { count, selectedButton } = useStore((state) => state);
return (
<div>
<h1>SecondChild</h1>
<p>카운트: {count}</p>
<p>선택한 버튼: {selectedButton}</p>
</div>
);
}
export default SecondChild;
App.jsx
import FirstChild from "./components/FirstChild";
import SecondChild from "./components/SecondChild";
function App() {
return (
<div>
<FirstChild />
<SecondChild />
</div>
);
}
export default App;
store.ts
// store.ts
import create from "zustand";
interface Store {
selectedButton: string | null;
count: number;
setSelectedButton: (button: string) => void;
incrementCount: () => void;
removeCount: () => void;
}
const useStore = create<Store>((set) => ({
selectedButton: null,
count: 0,
setSelectedButton: (button) => set({ selectedButton: button }),
incrementCount: () => set((state) => ({ count: state.count + 1 })),
removeCount: () => set({ count: 0 }),
}));
export default useStore;
FirstChild.jsx
// FirstChild.tsx
import React from "react";
import useStore from "store/store";
function FirstChild() {
const setSelectedButton = useStore((state) => state.setSelectedButton);
const incrementCount = useStore((state) => state.incrementCount);
const removeCount = useStore((state) => state.removeCount);
const handleClick = (button: string) => {
setSelectedButton(button);
};
return (
<div>
<h1>FirstChild</h1>
<div>
<button onClick={() => handleClick("O")}>O</button>
<button onClick={() => handleClick("X")}>X</button>
</div>
<div>
<button onClick={incrementCount}>카운트 증가</button>
<button onClick={removeCount}>카운트 리셋</button>
</div>
</div>
);
}
export default FirstChild;
SecondChild.jsx
// SecondChild.tsx
import React from "react";
import useStore from "store/store";
function SecondChild() {
const selectedButton = useStore((state) => state.selectedButton);
const count = useStore((state) => state.count);
return (
<div>
<h1>SecondChild</h1>
<p>카운트: {count}</p>
<p>선택한 버튼: {selectedButton}</p>
</div>
);
}
export default SecondChild;
App.tsx
import FirstChild from "components/FirstChild";
import SecondChild from "components/SecondChild";
function App() {
return (
<div className="App">
<FirstChild />
<SecondChild />
</div>
);
}
export default App;
References
좋은정보 감사합니다