Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么在redux中使用扩展运算符_Javascript_Reactjs_Redux_React Redux - Fatal编程技术网

Javascript 为什么在redux中使用扩展运算符

Javascript 为什么在redux中使用扩展运算符,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我看到了以下代码: export default function productReducer(state = initialState, action) { switch(action.type) { case FETCH_PRODUCTS_BEGIN: return { ...state, loading: true, error: null

我看到了以下代码:

export default function productReducer(state = initialState, action) {
    switch(action.type) {
        case FETCH_PRODUCTS_BEGIN:
            return {
                ...state,
                loading: true,
                error: null
            };

        case FETCH_PRODUCTS_SUCCESS:
            return {
                ...state,
                loading: false,
                items: action.payload.products
            };

        case FETCH_PRODUCTS_FAILURE:
            return {
                ...state,
                loading: false,
                error: action.payload.error,
                items: []
            };

        default:
            return state;
    }
}
但我不明白为什么我们每次都用…陈述每个案例

我们不能写下:

return {
                    loading: false,
                    error: action.payload.error,
                    items: action.payload.products
                };

任何人都可以解释这一点吗?

因为通常您希望将其他密钥保留在您的状态中

如果你所在州:

{
   items:['a', 'b', 'c'],
   loading: false,
   error: null
}
您只返回例如:

case FETCH_PRODUCTS_BEGIN:
    return {
        // ...state, --> Without this line
        loading: true,
        error: null
    };
你的新州将是

{
    loading: true,
    error: null
}
您的
项目将丢失

然后,返回:

case FETCH_PRODUCTS_BEGIN:
    return {
        ...state,
        loading: true,
        error: null
    };
你是说

返回状态副本,但覆盖
加载
错误


因为通常情况下,您希望在您的州内保留其他密钥

如果你所在州:

{
   items:['a', 'b', 'c'],
   loading: false,
   error: null
}
您只返回例如:

case FETCH_PRODUCTS_BEGIN:
    return {
        // ...state, --> Without this line
        loading: true,
        error: null
    };
你的新州将是

{
    loading: true,
    error: null
}
您的
项目将丢失

然后,返回:

case FETCH_PRODUCTS_BEGIN:
    return {
        ...state,
        loading: true,
        error: null
    };
你是说

返回状态副本,但覆盖
加载
错误


这用于创建具有新值或更新值的新复制状态对象(如果没有此选项,则需要手动指定每个状态字段)

可作为替代方案


对使用扩展运算符有很好的解释。

这是用于创建具有新值或更新值的新复制状态对象(如果没有它,则需要手动指定每个状态字段)

可作为替代方案


对使用spread运算符有很好的解释。

它不是强制性的,用于保留以前的现有KeyDocumentation reference:或更进一步:它不是强制性的,用于保留以前的现有KeyDocumentation reference:或更进一步: