부모↔자식
자식↔자식
이어져있지 않은 다른 컴포너트 간도 OK
기존 MVC 모델의 한계를 극복
대규모 어플리케이션에서 데이터 흐름을 일관성 있게 관리함으로써 프로그램 예측가능성을 높임
MVC패턴
기본 개념
{
type: 'ACTION_CHANGE_USER', // 필수
payload: { // 옵션
name: '하나몬',
age: 100
}
}
dispatch()
메소드 사용!Redux Toolkit을 사용하자 (slice)
특정 기능에 대한 슬라이스를 생성하면import { createSlice } from '@reduxjs/toolkit'; const msgboxSlice = createSlice({ name: 'msgbox', initialState: { open: false, message: '', }, reducers: { open(state, action) { state.open = true; state.message = action.payload }, close(state) { state.open = false; } } }); export default msgboxSlice; // reducer: msgboxSlice.reducer // action creators: msgboxSlice.actions.open, msgboxSlice.actions.close // actionType: // - msgboxSlice.actions.open.type: 'msgbox/open' // - msgboxSlice.actions.close.type: 'msgbox/close'
아래와 같이 액션타입, 액션크리에이터, 리듀서를 만들어줘야하는 보일러플레이트 코드를 줄일 수 있다.
export const OPEN = 'msgbox/OPEN'; export const CLOSE = 'msgbox/CLOSE'; export const open = (message) => ({ type: OPEN, message }); const initialState = { open: false, message: '', }; export default msgbox(state = initialState, action) { switch (action.type) { case OPEN: return { ...state, open: true, message: action.message }; case CLOSE: return { ...state, open: false }; default: return state; } }
리디기술블로그 발췌
잘 봤습니다. 좋은 글 감사합니다.