티스토리 뷰
(1) redux 설치
npm install redux react-redux
(2) redux-thunk 설치
npm install redux-thunk
(3) index.js
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter } from "react-router-dom";
import { Provider } from "react-redux";
import store from "./redux/store";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>
);
reportWebVitals();
(3) redux/store.js 스토어 생성
//store 생성 함수
import { createStore } from "redux";
import reducer from "./reducer/reducer";
//store는 reducer를 매개변수로 가져온다.
let store = createStore(reducer);
export default store;
(4) redux/reducer/reducer.js
reducer는 여러 개를 만들 수 있다.
//관리 해줄 state(객체)
let initialState = {};
//reducer는 항상 state와 action 파라미터 2개를 가져간다.
//state는 초기값으로 initialState를 넣어준다.
function reducer(state = initialState, action) {}
export default reducer;
(5) redux/store.js redux-thunk 추가
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import rootReducer from './reducers/index'
const store = createStore(rootReducer, applyMiddleware(thunk))
'프론트엔드 > React' 카테고리의 다른 글
ContextAPI() (0) | 2022.06.04 |
---|---|
상태관리 (0) | 2022.06.04 |
async, await (0) | 2022.06.04 |
Promise (0) | 2022.06.04 |
Redux Middleware (0) | 2022.06.02 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- EVERY
- 얕은복사
- 기본형 데이터
- 불변 객체
- 참조형 데이터
- findindex
- redux middleware
- null
- redux
- redux-thunk
- 타입변환
- 비교 연산자
- foreach
- 동적(dynamic) 언어
- 느슨한 타입(loosely typed)
- filter
- redux thunk
- redux-middleware
- some
- find
- undefined
- map
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함