Migrating to redux-toolkit a switch multiply-case code

Reading about redux-toolkit and thinking that it's a great tool. But how to deal if you have to migrate from a redux switch multi-cases written code? Is there way not to duplicate action methods that are described with cases as logical OR?

    const someReducer = (state = {result:1, error: null, text: ""}, action) => {
    switch(action.type){
        case 'ADD':
        case 'SUM':
        case 'TOTAL':
            return {
                ...state,
                result: state.result + action.payload,
                error: null
            };
        case 'DIVIDE':
            return {
                ...state,
                error: true,
                text: "some error"
            };
        case 'MULTIPLY':
            return {
                ...state,
                result: state.result*ation.payload,
                error: null
            };
        default: 
            return state
    }
};

So after migrating to RTK it will looks not so handy with duplicating same code:

const someReducer = createSlice({
name: 'someReducer', 
initialState: {result:1, error: null, text: ""},
reducers: {
    add: (state, { payload }) => { result.state += payload, result.error=null }
    total: (state, { payload }) => { result.state += payload, result.error=null }
    sum: (state, { payload }) => { result.state += payload, result.error=null }
    divide: (state, { payload }) => { result.error=null, result.tex t= "some error" }
    multiply: (state, { payload }) => { result.state= result.state * payload, result.error=null  }
}});


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation