
그 새로운 도구가 바로 리덕스 입니다. 우리가 리덕스를 사용하면State를 공유하고자 할때 부-모 관계가 아니여도 되고, 중간에 의미없이 컴포넌트를 거치지 않아도 됩니다. 그리고 자식 컴포넌트에서 만든 State를 부모 컴포넌트에서도 사용할 수 있게 된다.

yarn add redux react-reduxsrc 폴더 안에 redux 폴더를 생성redux 폴더 안에 config, modules 폴더를 생성config 폴더 안에 configStore.js파일을 생성합니다. 각각의 폴더와 파일은 역할이 있습니다.
redux : 리덕스와 관련된 코드를 모두 모아 놓을 폴더 입니다.config : 리덕스 설정과 관련된 파일들을 놓을 폴더 입니다.configStore : “중앙 state 관리소" 인 Store를 만드는 설정 코드들이 있는 파일 입니다.modules : 우리가 만들 State들의 그룹이라고 생각하면 됩니다. 예를 들어 투두리스트를 만든다고 한다면, 투두리스트에 필요한 state들이 모두 모여있을 todos.js를 생성하게 되텐데요, 이 todos.js 파일이 곧 하나의 모듈이 됩니다.src/configStore.js
import { createStore } from "redux";
import { combineReducers } from "redux";
const rootReducer = combineReducers({});
const store = createStore(rootReducer);
export default store;
index.js
// 원래부터 있던 코드
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();
중앙 State 관리소를 가지고 있으며, 모든 State는 이곳에서 생성된다.redux,react-redux가 필요하다.