reducer 动作可以成为 redux 工具包中的嵌套对象吗?

Can reducer actions to be nested objects in redux toolkit?

我是 Redux 的新手,想知道是否可以将操作嵌套在层次结构中以提高可读性:

const authSlice = createSlice({
    name: 'auth',
    initialState: {
        data: {
            token: null
        },
        status: 'idle',
        error: null
    },
    reducers: {
        login: {
            requested: state => {
                state.status = 'loading';
            },
            received: (state, action) => {
                state.token = action.payload.token;
                state.status = 'succeeded';
            },
            failed: state => {
                state.status = 'failed'
            }            
        }
    }
});

与此相反:

const authSlice = createSlice({
    name: 'auth',
    initialState: {
        data: {
            token: null
        },
        status: 'idle',
        error: null
    },
    reducers: {
        loginRequested: state => {
            state.status = 'loading';
        },
        loginReceived: (state, action) => {
            state.token = action.payload.token;
            state.status = 'succeeded';
        },
        loginFailed: state => {
            state.status = 'failed'
        }

    }
});

我不知道这是否是一个问题,我只是没能找到这方面的例子。

不,库不支持 - 这就是为什么您找不到任何示例的原因。

资料来源:我是维护者 ;)