useReducer() and scale up ReactJs with useContext()
//The reducer function uses (initial) state and (dispatch) action as arguments.
//some cases return update the state, other replace it with _action_ properties
function reducer(state, action) {
switch (action.type) {
case 'incremented_age': {
return {
name: state.name,
age: state.age + 1
};
}
case 'changed_name': {
return {
name: action.nextName,
age: state.age
};
}
}
throw Error('Unknown action: ' + action.type);
}
const initialState = { name: 'Taylor', age: 42 };
const [state, dispatch] = useReducer(reducer, initialState);Scaling up React components with useContext() and useReducer()

PreviousREACT 4, useMemo(), useReducer(), ReactJs scaling and useRef(()=>{}) Dom manipulation.NextREACT 5, useRef() instance methods, React keyframe animations, useRef() scroll, onDrag() and onDrop() React Events
Last updated