๐Ÿ’ป์›น(Web)/React

[React]Vite, Redux

stonesy 2024. 6. 27. 16:42
728x90

CRA๋ž€ JavaScript๋กœ ๊ตฌ์„ฑ๋œ Webpack(์›นํŒฉ)์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์†๋„๊ฐ€ ๋Š๋ฆฐํŽธ์ด๋‹ค. ์ฝ”๋“œ์˜ ์–‘์ด ๋งŽ์•„์งˆ์ˆ˜๋ก ๋Š๋ฆฐ ์†๋„๋ฅผ ์ฒด๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„์˜ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Esbuild๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๋นŒ๋“œํˆด์ธ vite๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

*Esbuild: Go ์–ธ์–ด๋กœ ์ž‘์„ฑ๋œ JavaScript ๋นŒ๋“œํˆด, ์†๋„๊ฐ€ ๋น ๋ฆ„

 

๐Ÿ“ŒWebpack๊ณผ Vite์˜ ์ฐจ์ด์ 

๊ฐœ๋ฐœ์„œ๋ฒ„

  • Webpack: ์†Œ์Šค ์ฝ”๋“œ์™€ ๋ชจ๋“  ์ข…์† ๊ด€๊ณ„์˜ ๋ชจ๋“  ๋ฒˆ๋“ค๋ง ํ›„ ์„œ๋ฒ„๊ฐ€ ์ค€๋น„๋œ๋‹ค.
  • Vite: Esbuild๋กœ ๋ฏธ๋ฆฌ ๋ฒˆ๋“ค๋งํ•œ ๋ชจ๋“ˆ์„ ํ•„์š”ํ•  ๋•Œ ๋™์ ์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์ฆ‰๊ฐ์ ์œผ๋กœ ์„œ๋ฒ„๊ฐ€ ๊ตฌ๋™๋œ๋‹ค.

ํ”„๋กœ๋•์…˜ ๋นŒ๋“œ

  • Webpack: ๊ฐ ๋ชจ๋“ˆ์„ ๋ฒ”์œ„๋งˆ๋‹ค ํ•จ์ˆ˜๋กœ ๋งคํ•‘ํ•˜์—ฌ ๊ฒฐํ•ฉํ•œ๋‹ค.
  • Vite: ํ•˜๋‚˜์˜ ํŒŒ์ผ์— ๋ชจ๋“  ์ข…์† ๋ชจ๋“ˆ์„ ์ „์—ญ ๋ฒ”์œ„๋กœ ์„ ์–ธํ•˜์—ฌ ๊ฒฐํ•ฉํ•œ๋‹ค. ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋ณ๊ณ  ๋น ๋ฅด๊ฒŒ ๋นŒ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค.

 

npm init vite

⇒ ์ด ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด์„œ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ณ , ์˜ต์…˜ ํ•˜๋‚˜์”ฉ ์„ ํƒํ•˜๋ฉด ๋˜๋Š”๋“ฏ?

 

Redux Toolkit

1. Redux ์Šคํ† ์–ด(Store)

Redux ์Šคํ† ์–ด๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ „์ฒด ์ƒํƒœ(state)๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด์ด๋‹ค. ์Šคํ† ์–ด๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ๋ฅผ ๋ณด๊ด€ํ•˜๊ณ , ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋ฉฐ, ์ƒํƒœ์˜ ๋ณ€๊ฒฝ์„ ๊ตฌ๋…ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

  • ์ƒํƒœ ์ €์žฅ: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ ํŠธ๋ฆฌ ์ „์ฒด๋ฅผ ์ €์žฅํ•œ๋‹ค.
  • ์ƒํƒœ ์ ‘๊ทผ: getState()๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ˜„์žฌ ์ƒํƒœ๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ƒํƒœ ์—…๋ฐ์ดํŠธ: dispatch(action) ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๋‹ค. action์€ ์ƒํƒœ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ณ€๊ฒฝํ• ์ง€ ์„ค๋ช…ํ•˜๋Š” ๊ฐ์ฒด๋‹ค.
  • ๊ตฌ๋… ๊ด€๋ฆฌ: subscribed(listener) ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒํƒœ ๋ณ€๊ฒฝ์„ ๊ตฌ๋…ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ๋ฆฌ์Šค๋„ˆ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

์Šคํ† ์–ด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Redux์˜ ‘createStore’ ํ•จ์ˆ˜ ๋˜๋Š” Redux Toolkit์˜ ‘configureStore’ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. Redux Toolkit์˜ ‘configureStore’๋Š” ์Šคํ† ์–ด ์„ค์ •์„ ๋” ์‰ฝ๊ฒŒ ํ•ด์ฃผ๋Š” ๋„๊ตฌ์ด๋‹ค.

2. ๋ฆฌ๋“€์„œ(Reducer)

๋ฆฌ๋“€์„œ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜์ด๋‹ค. ๋ฆฌ๋“€์„œ๋Š” ํ˜„์žฌ ์ƒํƒœ์™€ ์•ก์…˜(action)์„ ์ธ์ˆ˜๋กœ ๋ฐ›์•„์„œ ์ƒˆ๋กœ์šด ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ฆฌ๋“€์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ–๋Š”๋‹ค.

  • ์ˆœ์ˆ˜ ํ•จ์ˆ˜: ์ฃผ์–ด์ง„ ์ธ์ˆ˜ ์™ธ์— ๋‹ค๋ฅธ ๊ฐ’์„ ์ฐธ์กฐํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜, ๋™์ผํ•œ ์ธ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๋ฉด ํ•ญ์ƒ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ํ˜„์žฌ ์ƒํƒœ์™€ ์•ก์…˜: ๋ฆฌ๋“€์„œ๋Š” ํ˜„์žฌ ์ƒํƒœ์™€ ์•ก์…˜์„ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค.
  • ์ƒˆ๋กœ์šด ์ƒํƒœ ๋ฐ˜ํ™˜: ๋ฆฌ๋“€์„œ๋Š” ์ƒˆ๋กœ์šด ์ƒํƒœ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ƒํƒœ๋Š” ๋ถˆ๋ณ€์„ฑ์ด ์œ ์ง€๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ๊ธฐ์กด ์ƒํƒœ๋ฅผ ์ง์ ‘ ๋ณ€๊ฒฝํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค. ๋Œ€์‹  ์ƒˆ๋กœ์šด ์ƒํƒœ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.

 

1. Redux ๋ฐ ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

npm install @reduxjs/toolkit react-redux

 

2. Redux ์„ค์ •

2-1. ์Šคํ† ์–ด ์ƒ์„ฑ

‘src’ ๋””๋ ‰ํ† ๋ฆฌ ์•„๋ž˜์— ‘app’ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ , ‘store.js’ ํŒŒ์ผ์„ ์ƒ์„ฑ

// src/app/store.js
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';

export const store = configureStore({
  reducer: {
    counter: counterReducer,
  },
});

 

2-2. ์Šฌ๋ผ์ด์Šค ์ƒ์„ฑ

‘src/features/counter’ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ , ‘counterSlice.js’ ํŒŒ์ผ์„ ์ƒ์„ฑ

// src/features/counter/counterSlice.js
import { createSlice } from '@reduxjs/toolkit';

export const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    value: 0,
  },
  reducers: {
    increment: (state) => {
      state.value += 1;
    },
    decrement: (state) => {
      state.value -= 1;
    },
  },
});

export const { increment, decrement } = counterSlice.actions;

export default counterSlice.reducer;

2-3. Redux Provider ์„ค์ •

‘src/main.jsx’ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์—ฌ Redux Provider๋ฅผ ์ถ”๊ฐ€

// src/main.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { store } from './app/store';
import App from './App';
import './index.css';

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

2-4. ์ปดํฌ๋„ŒํŠธ์—์„œ Redux ์‚ฌ์šฉ

์˜ˆ์‹œ๋กœ ‘src/App.jsx’ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์—ฌ Redux ์ƒํƒœ๋ฅผ ์‚ฌ์šฉ

import { useSelector, useDispatch } from "react-redux";
import { increment, decrement } from "./features/counter/counterSlice";

function App() {
  const count = useSelector((state) => state.counter.value);
  const dispatch = useDispatch();

  return (
    <>
      <h1>count: {count}</h1>
      <button onClick={() => dispatch(increment())}>Increment</button>
      <button onClick={() => dispatch(decrement())}>Decrement</button>
    </>
  )
}

export default App;

 

์ฐธ๊ณ 

[React] vite๋กœ react ํ”„๋กœ์ ํŠธ ์„ค์น˜

Redux Toolkit ์•Œ์•„๋ณด๊ธฐ

๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ๋ž€? - Webpack vs Vite ๋ฌด์—‡์„ ์จ์•ผ ํ• ๊นŒ์š”?

 

728x90