React | Redux toolkit

์ƒ˜์ƒ˜ยท2023๋…„ 4์›” 29์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
15/31
post-thumbnail

๐Ÿ”Ž Redux
props ์—†์ด state๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ


์„ค์น˜ํ•˜๊ธฐ

npm install @reduxjs/toolkit react-redux

ํ„ฐ๋ฏธ๋„์— ์œ„ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด์„œ ์„ค์น˜ํ•œ๋‹ค


์…‹ํŒ…ํ•˜๊ธฐ

// store.js
import { configureStore } from '@reduxjs/toolkit'

export default configureStore({
  reducer: { }
}) 

state๋“ค์„ ๋ณด๊ด€ํ•  ํŒŒ์ผ์ธ store.js๋ฅผ ์ƒ์„ฑํ•ด์„œ ์œ„ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ค€๋‹ค

// index.js
import { Provider } from "react-redux";
import store from './store.js'

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <Provider store={store}>
      <BrowserRouter>
        <App />
      </BrowserRouter>
    </Provider>
  </React.StrictMode>
); 

index.js ํŒŒ์ผ์— Provider ์ปดํฌ๋„ŒํŠธ์™€ ๋ฐฉ๊ธˆ ๋งŒ๋“ค์–ด์ค€ store ํŒŒ์ผ์„ importํ•˜๊ณ , App ์ปดํฌ๋„ŒํŠธ๋ฅผ Provider ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ์‹ธ์ค€๋‹ค

๐Ÿ‘‰ App ์ปดํฌ๋„ŒํŠธ๋ฅผ ํฌํ•จํ•œ ๋ชจ๋“  ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋“ค์€ store์— ์žˆ๋Š” state๋“ค์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ๊บผ๋‚ด์“ธ ์ˆ˜ ์žˆ๋‹ค


Redux store์— state ๋ณด๊ด€ํ•˜๊ธฐ

// store.js
import { configureStore, createSlice } from '@reduxjs/toolkit'

let user = createSlice({
  name : 'user',
  initialState : {name : 'kim', age : 20},
    reducers : {
    changeName(state){
      state.name = 'park'
    }  
})

export let {changeName} = user.actions
                       
export default configureStore({
  reducer: {
    user : user.reducer
  }
}) 

store.js ํŒŒ์ผ์— createSlice๋ฅผ importํ•œ๋‹ค
user state๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด, ๋ณ€์ˆ˜ user๋ฅผ ์„ ์–ธํ•˜๊ณ  {name: 'state์ด๋ฆ„', initialState: 'state๊ฐ’', reducers: {ํ•จ์ˆ˜} }์„ ์ž…๋ ฅํ•œ๋‹ค

๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด slice์ด๋ฆ„.actions๋กœ export ํ•ด์ค€๋‹ค

๋งŒ๋“ค์–ด์ค€ state๋Š” configureStore์— {state์ด๋ฆ„.reducer}๋กœ ๋“ฑ๋กํ•˜๊ณ , export ํ•ด์ค€๋‹ค


state ์‚ฌ์šฉํ•˜๊ธฐ

// Cart.js
import { useDispatch, useSelector } from 'react-redux'

state๋ฅผ ์‚ฌ์šฉํ•  ์ปดํฌ๋„ŒํŠธ์— useDispatch์™€ useSelector๋ฅผ import ํ•œ๋‹ค

// Cart.js
    let state = useSelector((state) => state)
    console.log(state)

    let dispatch = useDispatch()

state๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด, useSelector๋กœ store์— ์žˆ๋Š” ๋ชจ๋“  state๋“ค์„ ํ• ๋‹นํ•œ๋‹ค
์œ„ state๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด, dispatch ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค
store.js์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ dispatch๋กœ ๊ฐ์‹ธ์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•จ!

//Cart.js
import { changeName } from "./../store.js"

์‚ฌ์šฉํ•  ํ•จ์ˆ˜๋„ import ํ•ด์•ผ ํ•œ๋‹ค

// Cart.js html ๋ถ€๋ถ„
    return (
        <div>
            {state.user.name} {state.user.age}์˜ ์žฅ๋ฐ”๊ตฌ๋‹ˆ
            <button onClick={() => {
                dispatch(changeName())
            }}>๋ฒ„ํŠผ</button>

{}์•ˆ์— user state์˜ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ , ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด changeName ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ฒŒ ํ•œ๋‹ค

profile
ํšŒ๊ณ„ํŒ€ ์ถœ์‹  FE๊ฐœ๋ฐœ์ž ๐Ÿ‘‰์ฝ˜ํ…Œํฌ ํšŒ์‚ฌ์—์„œ ์›น๊ฐœ๋ฐœ์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€