// src/modules/user.js
//const ํ์
๋ช
= '์ฌ์ฉ ๋ reducer/ํ์
๋ช
'
export const ADD_USER = 'user/ADD_USER'
export const DELETE_USER = 'user/DELETE_USER'
action๊ฐ์ฒด :
{type: ~~ , payload: ~~}
์ ๊ฐ์ ๊ฐ์ฒด๋ฅผ redux์์๋ action๊ฐ์ฒด๋ผ๊ณ ํจdispatch : action๊ฐ์ฒด๋ฅผ
dispatch()
ํจ์๋ฅผ ํตํดreducer
๋ก ๋ณด๋ด๋ ํ์๋ฅผ dispatch๋ผ๊ณ ํจaction์์ฑํจ์ :
dispatch()
์ ์ ๋ฌ ํ ํ์ ๊ณผ ๋ฐ์ดํฐ๋ฅผ returnํ๋ ํจ์
dispatch({type: ~~, payload: ~~})
๋ผ๊ณ ์ฝ๋์์ ๋ช
๋ช
ํ๊ธฐ๋ณด๋จ action๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ ํจ์๋ฅผ ์์ฑํ์ฌ ํด๋น ํจ์๋ฅผ dispatch()
์ ์ ๋ฌ => dispatch(addUser(user))
payload
์ ์ธ์๋ก ๋ฐ์ ๊ธฐํ ๋ฐ์ดํฐ๋ค์ ๋ด์// src/modules/user.js
export function addUser(user) {
return {
type: ADD_USER,
payload: user,
};
}
export function deleteUser(userId) {
return {
type: DELETE_USER,
payload: { userId },
};
}
initialState
์ ๊ธฐ๋ณธ๊ฐ์ด ๋ ๋ฐ์ดํฐ๋ฅผ ๋ด์์ state
์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ ๋ฌstate
์ ๊ฐ์ ๋ถ๋ณ์ฑ์ ์ ์งํด์ผํจ math.random()
, new Date()
๋ฑ.. ์ ์์ดํ
์ฌ์ฉ๊ธ์งstate
๊ฐ์ ๋ณํ๋ฅผ ์์์ฐจ๋ฆฐ๋ค.push
๋ฑ ๋ฉ๋ชจ๋ฆฌ๊ฐ์ ๋ณํ๊ฐ ์๋ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ ๋ณ๊ฒฝ ๊ธ์งconcat
ํน์ ...
(spread) ์ฌ์ฉ// src/modules/user.js
const initialState = [
{
id: 1,
name: "Leanne Graham",
email: "Sincere@april.biz",
},
{
id: 2,
name: "Ervin Howell",
email: "Shanna@melissa.tv",
},
{
id: 3,
name: "Clementine Bauch",
email: "Nathan@yesenia.net",
}
]
export default function user(state = initialState, action) {
switch (action.type) {
case ADD_USER:
return [...state, action.payload];
case DELETE_USER:
return state.filter((item) => item.id !== action.payload.userId);
default:
return state;
}
}
Provider
๋ก ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ธ ์ด๋ค ์ปดํฌ๋ํธ์์๋ ๋ฆฌ๋์์ ์ํ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ํจ// src/store/index.js
import { legacy_createStore as createStore } from "redux";
import user from "../modules/user";
const store = createStore(user);
export default store;
ํ์ฌ
createStore
๋ deprecate๋์ด redux-toolkit์configureStore
๋ก ์ฌ์ฉ๋๊ธธ ๊ถ์ฅ๋์ง๋ง ์์ redux๋ง ์ฌ์ฉ ์ ์์ ๊ฐ์ดlegacy_createStore
๋ฅผ ๋ฐ์ ์ฌ์ฉ ํ ์ ์์
Provider
๋ก ์ฑ ์ ์ฒด๋ฅผ ๊ฐ์ผ ๋ค store
๋ฅผ ์ ๋ฌimport { Provider } from "react-redux";
import store from "./store";
import User from "./components/User";
function App() {
return (
<Provider store={store}>
<User />
</Provider>
);
}
export default App;
combineReducers()
๋ด์ ์ฌ์ฉ ํ Reducer๋ฅผ ๋ชจ๋ ์ ์ ํ ๋ค, ํด๋น ๋ณ์๋ฅผ store
์ ์ ๋ฌํ๋คconst rootReducer = combineReducers({ ํค: ๋ฆฌ๋์, ํค: ๋ฆฌ๋์, });
// src/store/index.js
import { combineReducers,legacy_createStore as createStore } from "redux";
import counter from "../modules/counter";
import user from "../modules/user";
const rootReducer = combineReducers({
counter,
user,
});
const store = createStore(rootReducer);
export default store;
Provider
๋ก App์ ์ฒด๋ฅผ ๊ฐ์ธ์ฃผ์ด store๋ฅผ ์ ๋ฌํด์ฃผ๋ฉด ๋!useSelector()
๋ reducer์ ์ ์ ๋ state๊ฐ์ ๋ฐ์์ดcombineReducers
์์ ์ ์ํ ํด๋น reducer์ key๊ฐ์ผ๋ก ์ํ๋ reducer์ state๋ฅผ ๋ถ๋ฌ์จ๋ค//src/components/User
import { useSelector } from "react-redux";
function User() {
// reducer๊ฐ ํ๊ฐ์ผ ๋ (๊ธฐ๋ณธ)
const users = useSelector((state) => state);
// reducer๊ฐ ์ฌ๋ฌ๊ฐ์ผ ๋
const users = useSelector((state) => state.user);
const number = useSelector((state) => state.counter);
return ...
}
action์์ฑํจ์
๋ฅผ ์ ๋ฌํด์ฃผ๋ ๋ฐฉ์์ด ์ฃผ๋ก ์ฌ์ฉ ๋จimport { useDispatch, useSelector } from "react-redux";
import { addUser, deleteUser } from "../modules/user";
function User() {
const users = useSelector((state) => state.user);
const dispatch = useDispatch();
const onAdd = (user) => {
dispatch(addUser(user));
};
const onDelete = (userId) => {
dispatch(deleteUser(userId));
};
return (
<div>
{users.map((user) => (
<div key={user.id}>
<p>{user.name}</p>
<button onClick={() => onDelete(user.id)}>์ ๊ฑฐ</button>
</div>
))}
<button onClick={() => {onAdd({ name: "john" })}}>์ถ๊ฐ</button>
</div>
);
}
export default User;
Redux ๋ณ ๊ฑฐ ์๋ค!!!
...
๋ผ๊ณ ์๊ฐํ์๋ค.... ์ด๋๊น์ง๋.......๐ฅฒ
๋ฑ ์ด๋งํผ๋ง ์๋ฉด ์๋๋๊ฑธ๊น์......
๊ทธ๋๋ ์ฒ์ ๋ฆฌ์กํธ๋ฅผ ๋ฐฐ์ธ ๋ ์ด ์ปดํฌ๋ํธ์์ ์ ์ปดํฌ๋ํธ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ง๊ตฌ๋ง๊ตฌ ์ฃผ๊ณ ๋ฐ์์ผํ๋๊ฒ ๊ทธ๋ ๊ฒ ๊ณจ์น๊ฐ ์ํ ์ ์์๋๋ฐ ์ด๋ฐ ๋๊ตฌ๋ค์ ์ฌ์ฉํ ์ค ์๊ฒ ๋๋ฉด์ ํ๊ฒฐ ๋ค๋ฃจ๊ธฐ๊ฐ ์์ํด์ง ๋๋์ด๊ธด ํ๋ค..!!
๋ฐฐ์ฐ๋๊ฒ ์ด๋ ต๊ธด ํ์ง๋ง ์๋๊ฒ ๋ง์์ง์๋ก ์ฝ๋ ๋ค๋ฃจ๊ธฐ๊ฐ ์ฌ์์ง๋๊ฒ ๋๊ปด์ง๋... ์ด์ฌํ ํด๋ด์ผ๊ฒ ์ฃ ..!!?
์ผ๋จ ์ฃผ๋ง์ ์ฌ๊ณ ๋ค์์ฃผ์๋ redux์ middleware์ toolkit๋ ์ ๋ฆฌํด๋ด์ผ์ง... ๐ฅฒ (๊ทธ๋งํ๊ณ ์ถ์ด์...๐ซ )