728x90
300x250
- 리덕스 설치
- npm install redux --save
- 리덕스 사용법
const redux = require('redux'); //redux import
const counterReducer = (state = { counter: 0 } , action) => {
if (action.type === 'increment') {
return {
counter: state.counter + 1;
};
}
if (action.type === 'decrement') {
return {
counter: state.counter - 1;
};
}
return state;
}; //reducer 생성 (Inputs: Old State + Dispatched Action, Output: New State Object)
const store = redux.createStore(counterReducer); //저장소 생성 (리듀서 함수 가리키기)
const counterSubscriber = () => {
store.getState()
}; //구독
store.subscribe(counterSubscriber); //구독 함수 가리키게
store.dispatch({ type: 'increment' }); //액션 발송
store.dispatch({ type: 'decrement' }); //액션 발송
728x90
300x250
'React > Redux' 카테고리의 다른 글
Redux - 리엑트 컴포넌트 안에서 리덕스 데이터 사용 (0) | 2023.07.10 |
---|---|
Redux 시작 환경 (0) | 2023.07.10 |
Redux란? (0) | 2023.07.10 |