简体   繁体   English

动作有效负载被使用了两次(Redux)

[英]Action payload is being used twice (Redux)

I am creating a server-side rendered React app that uses Redux and fetches content from a Wordpress installation (through the WP API). 我正在创建一个使用Redux并从Wordpress安装中获取内容的服务器端渲染React应用程序(通过WP API)。 Please consider the following reducer: 请考虑以下减速器:

const initState = {
    mainMenu: null,
    userMenu: null
}

export default ( state = initState, action ) => {

    switch( action.type ) {

        case 'FETCH_MAIN_MENU':
            state = {
                ...state, 
                mainMenu: action.payload.data
            }           

        case 'FETCH_USER_MENU':
            state = {
                ...state, 
                userMenu: action.payload.data
            }                   

        default: 
            return state; 

    }

}

Both actions are dispatched on the server side, before the app is rendered for the first time, and both actions are promises that return two different menus. 这两个动作均在首次呈现应用程序之前在服务器端分派,并且这两个动作都是返回两个不同菜单的承诺。 The problem is that sometimes the payload for FETCH_USER_MENU is actually the same as the payload for FETCH_MAIN_MENU . 问题在于,有时FETCH_USER_MENU的有效负载实际上与FETCH_MAIN_MENU的有效负载相同。 Presumably, this happens when the promise issued by FETCH_USER_MENU resolves too slowly, and thus action.payload.data takes the value assigned to it in the FETCH_MAIN_MENU case. 大概是在FETCH_USER_MENU发出的承诺解决得太慢时发生这种情况,因此action.payload.dataFETCH_MAIN_MENU情况下采用FETCH_MAIN_MENU分配的值。 I'm pretty sure this is what's going on because if I arbitrarily set action.payload.data to null, the user menu (again, only sometimes) isn't rendered at all: 我很确定这是怎么回事,因为如果我将action.payload.data任意设置为null,则根本不会呈现用户菜单(再次,仅有时):

const initState = {
    mainMenu: null,
    userMenu: null
}

export default ( state = initState, action ) => {

    switch( action.type ) {

        case 'FETCH_MAIN_MENU':
            state = {
                ...state, 
                mainMenu: action.payload.data
            }           

        action.payload.data = null; 

        case 'FETCH_USER_MENU':
            state = {
                ...state, 
                userMenu: action.payload.data // Sometimes results as null
            }                   

        default: 
            return state; 

    }

}

Can you suggest a solution other than creating a different reducer for each menu, which I would like to avoid? 除了为每个菜单创建不同的reducer之外,您能否提出其他解决方案?

The problem is caused by Switch case fallthrough since you are not returning from the case statement nor using break . 由于您没有从case语句返回或不使用break,因此此问题是由Switch case fallthrough引起的。 Ideally with redux you need to return the updated state. 理想情况下,需要使用redux返回更新后的状态。 Your would write 你会写

export default ( state = initState, action ) => {

    switch( action.type ) {

        case 'FETCH_MAIN_MENU':
            return {
                ...state, 
                mainMenu: action.payload.data
            }           

        case 'FETCH_USER_MENU':
            return  {
                ...state, 
                userMenu: action.payload.data
            }                   

        default: 
            return state; 

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM