Redux quick expl
unknown
javascript
2 years ago
911 B
5
Indexable
// actions.js const addPost = (title, body) => ({ type: ADD_POST, payload: { title, body } }); // reducer.js function reducer(state=[], action){ switch(action.type){ case ADD_POST: return [...state, action.payload] } return state; } // store.js function createStore(reducerFunction, initialState) { let currentState = initialState; let listeners = []; return { dispatch() { currentState = reducerFunction(currentState, action); listeners.forEach((listenerFunction) => listenerFunction()); }, getState() { return currentState; }, subscribe(listener) { listeners.push(listener); return function () { listeners = listeners.filter((currentListener) => currentListener !== listener) } } } } const store = createStore(reducer); // Components store.dispatch(addPost("Pierwszy tytuł", "Pierwsza treść")); store.dispatch(addPost("Drugi tytuł", "Druga treść"));
Editor is loading...