Redux stores keep application state in one place and update it by passing actions through a reducer.
Implement a simplified Redux store. The createStore function only needs to initialize reducer state, expose getState(), and update state through dispatch().
Subscriptions are intentionally out of scope for this question and are added in the follow-up question.
function counterReducer(state = 0, action) {switch (action.type) {case 'increment':return state + 1;case 'decrement':return state - 1;default:return state;}}const store = createStore(counterReducer);store.getState(); // 0store.dispatch({ type: 'increment' });store.getState(); // 1store.dispatch({ type: 'decrement' });store.getState(); // 0
function counterReducer(state = 0, action) {if (action.type === 'increment') {return state + 1;}return state;}const store = createStore(counterReducer, 10);store.getState(); // 10store.dispatch({ type: 'increment' });store.getState(); // 11
createStore(reducer, preloadedState?)
reducer ((state, action) => nextState): Receives the current state and an action object, then returns the next state.preloadedState (unknown, optional): When provided, this value should be passed to the reducer during initialization.The store should initialize itself by calling reducer() once with an internal action whose type is @@redux-store/INIT.
Returns a store object with the following methods:
store.getState()Returns the current state stored by the reducer.
store.dispatch(action)Passes action to the reducer, stores the returned state, and returns the same action object.
subscribe() in this question.combineReducers, replaceReducer, action validation, or nested dispatch rules.Implement Redux Store II to add subscriptions and dispatch-time listener semantics.
console.log() aparecerão aqui.