"Single source of truth"
진실은 하나의 근원으로부터

{
type: UPDATE_GUESTS,
guests: {
adult,
child,
infant,
},
}
type이라는 키를 필수적으로 가져야 한다. 그 외의 값은 자유롭게 지정할 수 있다.export const updateGuests = ({ adult, child, infant }) => ({
type: UPDATE_GUESTS,
guests: {
adult,
child,
infant,
},
});
액션 생성함수는 말 그대로 액션을 만드는 함수이다. 인자를 받아 액션 객체를 리턴하게 된다.
액션 생성함수를 만들어서 사용하는 이유는 컴포넌트에서 쉽게 액션을 발생시키기 위함이다. 그래서 액션 생성함수 앞에 export를 해주는 것이다.
export default function guestCount(state = INITIAL_STATE, action) {
switch (action.type) {
case UPDATE_GUESTS:
return {
...state,
guests: action.guests,
};
default:
return state;
}
}
import React from "react";
import ReactDOM from "react-dom";
import Routes from "./Routes";
import { Provider } from "react-redux";
import { createStore } from "redux";
import rootReducer from "./store/reducers";
const store = createStore(rootReducer);
ReactDOM.render(
<Provider store={store}>
<Routes />
</Provider>,
document.getElementById("root")
);
Provider를 통해 프로젝트 어디서든 접근할 수 있다.const dispatch = useDispatch()
스토어의 내장 함수로서 스토어에 액션 객체를 전달하는 함수이고, 액션 객체를 인자로 받아 온다.
dispatch가 실행되면 액션 객체는 리듀서로 전달되고, 리듀서 내에 미리 정의해둔 조건문과 action.type에 따라 스토어가 업데이트 된다.
const items = useSelector((store) => store.cartReducer);
액션 -> (미들웨어) -> 리듀서 -> 스토어 업데이트 -> 컴포넌트 업데이트