๐ Redux-toolkit
- ๋ฆฌ์กํธ์์๋ ๋ฐ๋์ ์ ์ญ ์ํ ๊ด๋ฆฌ๋ผ๋ ๊ฒ์ ํด์ผํ๋ค. ํ๋ ๋ฒ๋ง props๋ก ๋๊ธฐ๋ฉด ๊ด์ฐฎ์ง๋ง ์ ๊ทธ๋ด ๋๋ ๋ง์ผ๋,,
- ๋ณดํต ๋ง์ด ์ฌ์ฉํ๋ ๊ฒ Redux, MobX, ContextAPI, recoil, zustand์ธ๋ฐ ๋๋ Redux๋ก ํด๋ณด๊ธฐ๋ก ๊ฒฐ์ !
- ๊ฐ์ฅ ์ฌ์ฉ์๊ฐ ๋ง๊ธฐ๋ ํ๊ณ ๊ทธ๋ผ ์ ๋ณด๋ ๋ ๋ง์๊ฑฐ๋๊น ใ ใ
๐ Redux์ Redux-toolkit์ ์ฐจ์ด
- Redux๋ฅผ ๋ง๋ ๊ณณ์์ ๊ณต์์ ์ผ๋ก ํจ์จ์ ์ธ Redux ๊ฐ๋ฐ์ ์ํด ๋ง๋ค์ด์ง ํดํท
- Redux-Toolkit์ ์ฌ์ฉํ๋ฉด Redux์ ๋ฌธ์ ์ ํด๊ฒฐ
- Redux ์คํ ์ด ๊ตฌ์ฑ์ด ๋๋ฌด ๋ณต์กํ๋ค.
- Redux์์ ์ ์ฉํ ์์ ์ ์ํํ๋ ค๋ฉด ๋ง์ ํจํค์ง๋ฅผ ์ถ๊ฐํด์ผ ํ๋ค.
- Redux์๋ ๋๋ฌด ๋ง์ ์์ฉ๊ตฌ ์ฝ๋๊ฐ ํ์ํ๋ค.
๐ Redux-Toolkit์ด ์ ๊ณตํ๋ ๊ฒ
- configureStore : ๊ฐ๋จํ๊ฒ Store๋ฅผ ๋ง๋ค ์ ์์
- createReducer : switch๋ฌธ ์์ฑํ๋ ๊ฒ ๋์ ๊ฐ๋จํ๊ฒ Reducer๋ฅผ ๋ง๋ค ์ ์์
- createAction : Reducer์ ์์ฑํ ๊ฒ๋ค์ ๊ธฐ๋ฐ์ผ๋ก Action๋ค์ ๋ง๋ค์ด ์ค
- createSlice : reducer์ ์ด๋ฆ, ์ด๊ธฐ์ํ, reducers ๋ฑ์ ๊ฐํธํ๊ฒ ๋ง๋ค ์ ์๋๋ก ๋์์ค
- createAsyncThunk : createAction์ ๋น๋๊ธฐ๋ก ๋ง๋ค ์ ์๋๋ก ๋์์ค
- createSelector : Store์์ ์ํ๋ฅผ ํจ์จ์ ์ผ๋ก ์ ์ฅํ๋๋ก ๋์
1๏ธโฃ appํด๋์ redux ํด๋ ๋ง๋ค๊ณ store.js ๋ง๋ค๊ธฐ
import React from 'react';
import { configureStore } from '@reduxjs/toolkit';
export default configureStore({
reducer: {},
})
- configureStore์์ ๋ชจ๋ state๋ฅผ ๊ด๋ฆฌ
- reducer์ ์ํ๊ด๋ฆฌํ ๊ฒ๋ค ์ ์ฅ
2๏ธโฃ index.js์ Provider๋ก ๊ฐ์ธ๊ธฐ
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import reportWebVitals from './reportWebVitals';
import store from './redux/store';
import { Provider } from 'react-redux';
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById('root')
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
3๏ธโฃ ๋ฆฌ๋์ ๋ง๋ค๊ธฐ
- redux/user.js
import { createSlice } from '@reduxjs/toolkit';
export const userSlice = createSlice({
name: "user",
initialState: { value: {name: "", age: 0, email: ""}},
reducers: {
login: (state, action) => {
state.value = action.payload
},
},
});
export default userSlice.reducer;
- createReducer์ createAction์ด ํ๋ ์ผ์ ๊ฐ์ด ํจ
- ์ฌ์ฉํ ๋ฆฌ๋์์ ์ด๋ฆ ์ ํ๊ณ createSlice๋ก ์ง์
- name: ๋ฆฌ๋์ ์ด๋ฆ ๋ญ๋ก ํ ์ง ์ ํจ
- initialState: ๋ค์ด๊ฐ ๋ฐ์ดํฐ์ ์ด๊น๊ฐ ์ก์์ค
- reducers: ์ํ๊ฐ ๋ณํ๋ฉด ์ด๋ป๊ฒ ์คํ๋ ์ง ์ ํจ
- state: ์ง์ ํด๋์ ์ด๊ธฐ๊ฐ์ value๋ฅผ ๊ฐ์ ธ์ค๋ ์ญํ
- actions: payload๋ type์ด ์์ --> ๋ฐ๊พธ๊ณ ์ถ์ ๋ฐ์ดํฐ๋ฅผ ์ํ๋ ๊ณณ์๋ค๊ฐ ๋๊ฒจ์ค
- app/redux/store.js ---> ์คํ ์ด์ ์ ์ฅ
import { configureStore } from '@reduxjs/toolkit'
import userReducer from './user'
export default configureStore({
reducer:{
user: userReducer
}
})
- ์คํ ์ด์ ์ ์ฅํด์ผ ์๋ํจ
4๏ธโฃ ์คํ๋๋์ง ํ์ธ
import { useSelector } from 'react-redux'
const user = useSelector((state) => state.user.value)
- useSelector๋ฅผ ์ด์ฉํด์ ๋ง๋ ๋ฆฌ๋์์ ์ ๊ทผ
- user๋ผ๊ณ ์ด๋ฆ ์ง์ด์ค ๋ฆฌ๋์์ ์๋ state์ ์ ๊ทผ
- user ๋ฆฌ๋์์์ ์ด๊น๊ฐ์ value๋ก ์ง์ -> user์ ์๋ value๋ฅผ ๊ฐ์ ธ์ฌ ๊ฒ
- --> state.๋ฆฌ๋์ ์ด๋ฆ.์ด๊น๊ฐ
'REACT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[REACT] ๋ผ์ดํ์ฌ์ดํด ๋ฉ์๋ (0) | 2022.12.19 |
---|---|
[REACT] state, props, ์ด๋ฒคํธ ๊ธฐ๋ณธ๊ฐ๋ (0) | 2022.12.16 |
[REACT] children, ๋น๊ตฌ์กฐํ ํ ๋น (0) | 2022.12.13 |
[REACT] ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ก state ํ ๋นํ๊ธฐ (0) | 2022.12.06 |