loading...

re: React Context with useReducer and Typescript. VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Thanks! Yes I know if you have more than three reducers the types for the main reducer can increase, and be hard to manage. I don't know how to i...
 

So I found this on a stack overflow post

function combineReducers(reducers) {
    return (state = {}, action) => {
        const newState = {};
        for (let key in reducers) {
            newState[key] = reducers[key](state[key], action);
        }
        return newState;
    };
}

all you have to do is pass an object with key and value is the reducer. I'm searching about generic to convert this function to be type-safe. But if you have any idea please share. Thank you!

code of conduct - report abuse