๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

react/redux

[React] ๋ฆฌ๋•์Šค ์‹œ์ž‘ํ•˜๊ธฐ (redux, react-redux)

๐Ÿ•ต๐Ÿป‍โ™€๏ธ ๋ฆฌ๋•์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

  1. ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„์˜ ํƒ„์ƒ
  • ์–‘๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„์€ ํ•œ ๊ฐœ์˜ ๋ชจ๋ธ์ด ์—ฌ๋Ÿฌ ๋ทฐ๋ฅผ ์กฐ์ž‘ํ•˜๊ฑฐ๋‚˜, ํ•œ ๊ฐœ์˜ ๋ทฐ๊ฐ€ ์—ฌ๋Ÿฌ ๋ชจ๋ธ์„ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ์ดํ•ดํ•˜๊ณ  ๋ฒ„๊ทธ๋ฅผ ์ฐพ๋Š” ๊ฒƒ์„ ๋ฐฉํ•ดํ•œ๋‹ค. ์ด์™€ ๊ฐ™์€ ์ด์œ ๋กœ ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„์ด ํƒ„์ƒํ–ˆ๋‹ค.
  • ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„(= redux๊ฐ€ ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹)์€ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•œ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ํ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ์˜ˆ์ธกํ•˜๊ธฐ ์‰ฝ๋‹ค.
  1. ํŽธ๋ฆฌํ•œ state ๊ด€๋ฆฌ
  • redux๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด react ์ปดํฌ๋„ŒํŠธ ๋“ค์€ ๊ฐ๊ฐ ๊ฐœ๋ณ„์ ์œผ๋กœ state๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ค.
  • redux๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด state๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ „์šฉ ์žฅ์†Œ(store)์—์„œ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์ปดํฌ๋„ŒํŠธ๋Š” ๊ทธ๊ฑธ ๋ณด์—ฌ์ฃผ๊ธฐ๋งŒ ํ•˜๋Š” ์šฉ๋„๋กœ ์“ฐ์ธ๋‹ค.

 

๐Ÿ•ต๐Ÿป‍โ™€๏ธ redux์ ์šฉ ๋ฐ ๊ฐœ๋…

โœ” ํ•„์š”ํ•œ ํŒจ์บ์ง€ ์„ค์น˜

npm install redux
npm install react-redux

โœ” action

์•ก์…˜์€ ๋ฌด์Šจ์ผ์ด ์ผ์–ด๋‚ฌ๋Š”์ง€ ์„ค๋ช…ํ•ด์ฃผ๋Š” ๊ฐ์ฒด์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด user๋ผ๋Š” state๋ฅผ ์ˆ˜์ •ํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ์•ก์…˜์€ SET_USER์™€ ๊ฐ™์ด ์„ ์–ธํ•˜๊ณ , setUser(user)๋ผ๋Š” ํ•จ์ˆ˜ํ˜•ํƒœ์˜ action creator์„ ๋งŒ๋“ค์–ด ์ฃผ๋ฉด ๋œ๋‹ค.

์•ก์…˜ ์„ ์–ธ (๋Œ€๋ถ€๋ถ„ ๋Œ€๋ฌธ์ž๋กœ ์„ ์–ธ)

const SET_WINNER = 'SET_WINNER';
const CLICK_CELL = 'CLICK_CELL'; 
const CHANGE_TURN = 'CHANGE_TURN';

์•ก์…˜์„ ๋ฐ˜ํ™˜ํ•˜๋Š” action creators(์•ก์…˜ ์ƒ์„ฑ ํ•จ์ˆ˜)

export const setWinner(user) {
	return {type: SET_WINNER, user}
}

โœ” reducer

  • reducer๋Š” state์™€ action์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์™€์„œ state๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
  • action์—๋Š” ์•ก์…˜ ์ƒ์„ฑ ํ•จ์ˆ˜์—์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.
  • return ์—์„œ ๋‹ค๋ฅธ state๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์ „๊ฐœ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • reducer๋ฅผ ์ž‘์„ฑํ•  ๋•Œ switch ๋ฌธ์„ ์ž์ฃผ ์‚ฌ์šฉํ•œ๋‹ค.
const reducer = (state = initState, action) => {
	switch (action.type) {
		case Action์ด๋ฆ„:
			return {
					...state,
					๋ฐ”๊ฟ€ state: ์ง€์ • ๊ฐ’
			}
	}
}
  • ๊ฐ ์ปดํฌ๋„ŒํŠธ ๋ณ„๋กœ ๋ฆฌ๋“€์„œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค๋ฉด ์ด๋ฅผ ํ†ตํ•ฉํ•ด์•ผ ํ•œ๋‹ค. ์ด๋•Œ combineReducers()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
import { combineReducers } from 'redux';
import memberDetail from './memberDetail';
import member from './member';

export default combineReducers({
    memberDetail,
    member,
});

โœ” store

  • ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ฐ€์ƒ ๊ณต๊ฐ„์ด๋‹ค.
  • combineReducers()๋ฅผ ํ†ตํ•ด ํ†ตํ•ฉ๋œ reducer์™€ initState(state ์ดˆ๊ธฐ๊ฐ’)๋ฅผ ์ค€๋น„ํ•œ๋‹ค.
import { createStore } from 'redux';
const store = createStore(rootReducer);
  • store์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ 'index.js'๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด ์ฃผ์–ด์•ผํ•œ๋‹ค.
  • store ์˜ต์…˜์„ ์„ค์ •ํ•œ <Provider> ํƒœ๊ทธ๋กœ App component๋ฅผ ๊ฐ์‹ธ์ค€๋‹ค.
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import rootReducer from './store/modules';

const store = createStore(rootReducer);

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

reportWebVitals()

โœ” dispatch

dispatch๋กœ action์„ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

import { setMembersToStore } from '../../store/modules/member';
import { useDispatch } from 'react-redux';

const dispatch = useDispatch();
const saveMembersToStore = data => dispatch(setMembersToStore(data));
...
saveMembersToStore(member);

< useDispatch์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ! >

 

[React] useSelector, useDispatch๋กœ state์— ์ ‘๊ทผํ•˜๊ธฐ

๐Ÿ”น react-redux yarn add react-redux ๋˜๋Š” npm install react-redux state๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•œ useSelector๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. action์„ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•œ useDispatch๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๐Ÿ”น useSelector connectํ•จ..

juhi.tistory.com


 

๐Ÿ˜ผ ์ „์ฒด ์ ์šฉ ์˜ˆ์‹œ github

 

Hyun-juhee/Sopt27-Web

Sopt 27๊ธฐ ์›นํŒŒํŠธ. Contribute to Hyun-juhee/Sopt27-Web development by creating an account on GitHub.

github.com

 

๋ฐ˜์‘ํ˜•