Its such an eye opener when you learn to create the tools you are using and get a glimpse of how some of its functionality works under the hood.
function createStore(reducer, initialState) {
let state = initialState;
function getState() {
return state;
}
function dispatch(action) {
state = reducer(state, action);
}
return { dispatch, getState };
}
function reducer(state = [], action) {
switch (action.type) {
case "add":
return [...state, action.payload];
default:
return state;
}
}
const store = createStore(reducer);
store.dispatch({ type: "add", payload: "learn to code" });
store.dispatch({ type: "add", payload: "react" });
store.getState();
//[ 'learn to code', 'react' ]
inspired by: typeofnandev
Top comments (2)
actually, your implementation only 10 lines. I think there is enough space left for
.subscribe()
.Yes your right lol I will be added subscribe next and possible try create the wrappers for react