{
type: "TOGGLE_VALUE"
}
action 객체는 type 필드를 가지고 있어야 함
- type: 어떤 형태의 액션이 실행될지 결정함
{
type:"ADD_TODO",
data:{
id:0
text:"redux"
},
text: "hi"
}
export const changeInput = text => ({
type:"CHANGE_INPUT",
text
});
컴포넌트에서 더욱 쉽게 액션을 발생시키기 위해 사용
- 어떤 변화를 일으킬 때마다 액션 객체 생성해야 하기 때문에 이로 인해 발생하는 실수들을 줄이기 위해서 함수로 만들어서 관리함.
const initialState = {
counter: 1
};
function reducer(state = initialState, action) {
switch (action.type) {
case INCREMENT:
return {counter: state.counter + 1};
default:
return state;
}
}
리듀서는 반드시 순수해야함
- 오로지 계산만 가능함 : side effect, 예기치 못한 일 발생, API 호출, 변경 모두 불가능함.
스토어가 하는 일
1) 애플리케이션 상태 저장
2) getState()를 통해 state에 접근
3) dispatch(action)을 통해 state 수정
4) subscribe(listener)을 통해 리스너 등록
const listener = () => {
console.log(‘상태가 업데이트됨‘);
}
const unsubscribe = store.subscribe(listener);
unsubscribe(); // 추후 구독을 비활성화할 때 함수를 호출