Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.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/24.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 如何在React/Redux中最好地初始化状态/设置第一个状态_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript 如何在React/Redux中最好地初始化状态/设置第一个状态

Javascript 如何在React/Redux中最好地初始化状态/设置第一个状态,javascript,reactjs,redux,Javascript,Reactjs,Redux,目前,我正在我的减速机中执行以下操作: export default function (state = null, action) { if(state==null) { state = [ { id: 1, first: "Bucky", last: "Roberts", age: 71,

目前,我正在我的减速机中执行以下操作:

export default function (state = null, action) {

    if(state==null)
    {
        state = [
            {
                id: 1,
                first: "Bucky",
                last: "Roberts",
                age: 71,
                description: "Bucky is a React developer and YouTuber",
                thumbnail: "http://i.imgur.com/7yUvePI.jpg"
            },
            {
                id: 2,
                first: "Joby",
                last: "Wasilenko",
                age: 27,
                description: "Joby loves the Packers, cheese, and turtles.",
                thumbnail: "http://i.imgur.com/52xRlm8.png"
            },
            {
                id: 3,
                first: "Madison",
                last: "Williams",
                age: 24,
                description: "Madi likes her dog but it is really annoying.",
                thumbnail: "http://i.imgur.com/4EMtxHB.png"
            }
        ]
    }

    switch (action.type) {
        case 'USER_DELETED':
            return state.filter(user => user.id !== action.userIdToDelete);
    }

    return state;
}

所以我检查state是否为null,如果为null,则填充它。是否有更好的方法,即不从内部填充减速器?在我看来,这是一个糟糕的做法…

而不是将其默认值设置为null-执行以下操作

        initialState = [
        {
            id: 1,
            first: "Bucky",
            last: "Roberts",
            age: 71,
            description: "Bucky is a React developer and YouTuber",
            thumbnail: "http://i.imgur.com/7yUvePI.jpg"
        },
        {
            id: 2,
            first: "Joby",
            last: "Wasilenko",
            age: 27,
            description: "Joby loves the Packers, cheese, and turtles.",
            thumbnail: "http://i.imgur.com/52xRlm8.png"
        },
        {
            id: 3,
            first: "Madison",
            last: "Williams",
            age: 24,
            description: "Madi likes her dog but it is really annoying.",
            thumbnail: "http://i.imgur.com/4EMtxHB.png"
        }
    ];

    export default function (state = initalState, action) {
     ...
    }

您应该始终将默认状态值设置为您期望的类型,因此这里的
function(state=[])
更好。将其设置为null是一种反模式


然后,对于作为默认初始状态的数据,您应该使用createStore中的第二个参数来设置初始状态:

谢谢。然而,我的第二个参数是
applyMiddleware(thunk,promise,logger)
那么这是错误的吗?当包含初始(预加载)状态时,apply中间件将成为第三个参数。因此,在我发送给createStore的链接(reducer、[previedState]、[enhancer])中,增强器是您的中间件。