์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- Data Structure
- JPA
- ํ์ด์ฌ
- Spring
- react
- Heap
- design pattern
- ๋ฐฑ์ค
- Java
- redis
- ์ด์์ฒด์
- Galera Cluster
- mongoDB
- ์๊ณ ๋ฆฌ์ฆ
- Proxy
- c์ธ์ด
- spring webflux
- ๋์์ธ ํจํด
- ์๋ฃ๊ตฌ์กฐ
- OS
- MySQL
- ๋คํธ์ํฌ
- ์๋ฐ
- ์ปดํจํฐ๊ตฌ์กฐ
- IT
- Algorithm
- C
- Kafka
- MSA
- JavaScript
- Today
- Total
์๋ ์ค
react์์ Zustand๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ ๋ณธ๋ฌธ
Zustand
GitHub - pmndrs/zustand: ๐ป Bear necessities for state management in React
๐ป Bear necessities for state management in React. Contribute to pmndrs/zustand development by creating an account on GitHub.
github.com
๋ฏธ๋ ๋ฆฌ๋์ค...
๋ฆฌ๋์ค์ ๋ณด์ผ๋ฌ ํ๋ ์ดํธ์ ๊ดํ ํผ๋ก๊ฐ์ด ์๋นํ๊ธฐ ๋๋ฌธ์
๊ฐ๋จํ state๋ฅผ ๋ค๋ฃจ๊ธฐ์ Zustand๋ ๊ต์ฅํ ์ ์ฉํ๋ค.
import create from 'zustand'
const useStore = create(set => ({
bears: 0,
increasePopulation: () => set(state => ({ bears: state.bears + 1 })),
removeAllBears: () => set({ bears: 0 })
}))
create๋ฅผ ํตํด์ store๋ฅผ ๋ง๋ค์ด ์ค ํ
๊ฐ ํจ์๋ฅผ ํตํด์ state๋ฅผ ๋ณํ์ํจ๋ค.
๊ทธ ์์ฒด๋ก ๋ฆฌ๋์์ ์ก์ ์ ๋์ฒดํ ์ ์๋ค.
function BearCounter() {
const bears = useStore(state => state.bears)
return <h1>{bears} around here ...</h1>
}
function Controls() {
const increasePopulation = useStore(state => state.increasePopulation)
return <button onClick={increasePopulation}>one up</button>
}
state๋ฅผ ํ์ธํ๊ฑฐ๋, ์ก์ ์ ์ฃผ๋ ๊ฒ๋ ์์ ๊ฐ์ด ํ ์ ์๋ค!
import shallow from 'zustand/shallow'
// Object pick, re-renders the component when either state.nuts or state.honey change
const { nuts, honey } = useStore(state => ({ nuts: state.nuts, honey: state.honey }), shallow)
// Array pick, re-renders the component when either state.nuts or state.honey change
const [nuts, honey] = useStore(state => [state.nuts, state.honey], shallow)
// Mapped picks, re-renders the component when state.treats changes in order, count or keys
const treats = useStore(state => Object.keys(state.treats), shallow)
์์ ๋ณต์ฌ์ ๋ํด์๋ ์ง์ํ๊ณ ์๋ค.
๋ฏธ๋ค์จ์ด๋ redux-like ๋ฌธ๋ฒ๋ค๋ ์ง์ํ๊ณ ์์ง๋ง
ํ์๋ ๊ทธ๋ฐ ๊ฒ๋ค์ ๋ฆฌ๋์ค์์ ํ๊ณ ์ถ๋ค...
'ReactJS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React-Query ์ฌ์ฉ๋ฒ (0) | 2021.12.03 |
---|---|
React Hook Form ์ฌ์ฉ๋ฒ (0) | 2021.12.02 |
React์์ dotenv๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ (0) | 2021.11.27 |
React connected-react-router, react-router-dom, history ๋ฒ์ ์ถฉ๋ (0) | 2021.11.23 |
TIL : React { Hooks } (0) | 2021.11.20 |