如何在Redux中重用reducer?

时间:2019-02-08 19:05:05

标签: redux

我有以下的减速器,可以设置宽度或高度

我在想这样的事情

const dimensionsReducer = (state = {width: 300, height: 250}, action)
    => {
    switch(action.type) {
        case 'SET_WIDTH':
            return {
                ...state,
                width: action.value
            }
        case 'SET_HEIGHT':
            return {
                ...state,
                height: action.value
            }
        default:
            return state;
    }
}

export default dimensionsReducer;

我想创建一个可以从预设列表中同时设置宽度和高度的变径器

const presets = {
    bigBox: {
        width: 500,
        height: 500
    },
    littleBox: {
        width: 250,
        height: 250
    }
}

const presetReducer = (state = {preset: ""}, action) => {
    switch (action.type) {
        case 'SET_PRESET':
            return {
                ...state,
                preset: action.value

                // how do I do this
                // set width presets[action.value].width
                // set height presets[action.value].height
            }
        default:
            return state;
    }
}

如何重新使用尺寸缩小器来设置宽度和高度?

0 个答案:

没有答案