createStoreadvancedconst store = createStore(reducer)
store.subscribe(() => console.log(store.getState()))
store.dispatch({ type: 'INCREMENT' }) // logs 1
store.dispatch({ type: 'INCREMENT' }) // logs 2Implementing createStore from scratch shows how Redux manages state, subscriptions, and dispatch in only ~20 lines.
The Redux store is a plain object with three methods:
getState() β returns the current statedispatch(action) β calls the reducer and notifies subscriberssubscribe(listener) β registers a callback; returns an unsubscribe functionfunction createStore(reducer) {
var state = reducer(undefined, { type: '@@INIT' })
var listeners = []
return { getState, dispatch, subscribe }
}
Challenge: Implement createStore(reducer) with all three methods.