(1) 리덕스 설치
리액트에서 리덕스를 사용하기 위해서는 2개의 패키지를 설치해야 합니다. vscode 터미널에서 아래 명령어를 입력해서 2개의 패키지를 설치하세요. 참고로 react-redux 라는 패키지는 리덕스를 리액트에서 사용할 수 있도록 서로 연결시켜주는 패키지 입니다.
yarn add redux react-redux
아래와 같은 의미
yarn add redux
yarn add react-redux
2) 폴더 구조 생성하기
위의 이미지와 같이 폴더 구조를 생성하세요.
각각의 폴더와 파일은 역할이 있습니다.
(1) 설정 코드 작성 시, 주의사항
(2) src/configStore.js
src/configStore.js 에 아래 코드를 입력하세요.
import { createStore } from "redux";
import { combineReducers } from "redux";
const rootReducer = combineReducers({});
const store = createStore(rootReducer);
export default store;
(3) index.js
디렉토리의 가장 최상단에 있는 index.js에 아래 내용을 입력하세요.
```jsx
// 원래부터 있던 코드
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
// 우리가 추가할 코드
import store from "./redux/config/configStore";
import { Provider } from "react-redux";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
//App을 Provider로 감싸주고, configStore에서 export default 한 store를 넣어줍니다.
<Provider store={store}>
<App />
</Provider>
);
// 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, react-redux 가 필요하다.
- 설정코드는 지금 당장 이해 할 필요가 없다.
- “중앙 State 관리소"를 Store (스토어)라고 부른다.
- 모듈이란, State들이 그룹이다.