Redux Toolkit

laluniaxยท2023๋…„ 11์›” 28์ผ
0

REACT

๋ชฉ๋ก ๋ณด๊ธฐ
17/20

๐Ÿ“– ๋ฆฌ๋•์Šค ํˆดํ‚ท (ToolKit)

  • ๋ฆฌ๋•์Šค๋ž‘ ๊ตฌ์กฐ , ํŒจ๋Ÿฌ๋‹ค์ž„ ๋˜‘๊ฐ™์Œ
    ๋ชจ๋“ˆ ํŒŒ์ผ๋งŒ ๋ฐ”๋€ ๊ฒƒ
    ๊ธฐ์กด ๋ฆฌ๋•์Šค๋ณด๋‹ค ๋” ํŽธํ•œ ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋จ (์ฝ”๋“œ ๋” ์ ์–ด์ง)

๋ชจ๋“ˆ ์„ค์น˜ :
yarn add react-redux @reduxjs/toolkit

์ฐจ์ด์ : Action Value์™€ Action Creator๋ฅผ ์ด์ œ ์ง์ ‘ ์ƒ์„ฑํ•ด์ฃผ์ง€ ์•Š๊ณ ,
Action Value, Action Creator, Reducer๊ฐ€ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์กŒ๋‹ค๋Š” ์ 

CreateSlice API

CreateSlice ๊ตฌ์กฐ

const counterSlice = createSlice({
	name: '', // ์ด ๋ชจ๋“ˆ์˜ ์ด๋ฆ„
	initialState : {}, // ์ด ๋ชจ๋“ˆ์˜ ์ดˆ๊ธฐ์ƒํƒœ ๊ฐ’
	reducers : {}, // ์ด ๋ชจ๋“ˆ์˜ Reducer ๋กœ์ง
})

counterSlice ๋ฆฌ๋“€์„œ ๊ฐ์ฒด ์•ˆ์—์„œ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜๊ฐ€ ๋ฆฌ๋“€์„œ์˜ ๋กœ์ง์ด ๋˜๋ฉด์„œ๋„ ๋™์‹œ์— Action Creator๊ฐ€ ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Action Value ๊นŒ์ง€ ํ•จ์ˆ˜์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ง!

// ์•ก์…˜ํฌ๋ฆฌ์—์ดํ„ฐ๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด export ํ•˜๊ณ 
export const { addNumber, minusNumber } = counterSlice.actions;
// reducer ๋Š” configStore์— ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด export default ํ•ฉ๋‹ˆ๋‹ค.
export default counterSlice.reducer;

export ํ›„ configStore์— ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋™์ผ

import { configureStore } from "@reduxjs/toolkit";
/**
 *  1. Slice Reducer import 
 */
import counter from "../modules/counterSlice";

// ๋ชจ๋“ˆ๋กœ ๊ตฌํ˜„ ํ›„, Store์— ๋ชจ๋“ˆ ์—ฐ๊ฒฐ 
const store = configureStore({
  reducer: { counter: counter, todos: todos },
});
	
export default store;

์Šฌ๋ผ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, Action Value, Action Creator, Reducer ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๊ธฐ ๊ฐ€๋Šฅ !

ํŒŒ์ผ ๊ตฌ์กฐ ์ฐธ๊ณ !

profile
grow constantly

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