简体   繁体   中英

How to insert variable inside Menu tag - REACTJS

I have a.js file containing the code for a context menù component:

const ContextMenuDialog = (props) => {

    //  my state declaration, other const, etc...
                
                
                
    const build_ITEMS_ContextMenu = () => {

            const A = [
                      {
                        key: "0",
                        label: "AA123BB",
                        disabled: true
                      },
                      {
                        key: "1",
                        label: "Show"
                      },
                      {
                        key: "2",
                        label: "Edit"
                      },
                      {
                        key: "3",
                        label: "Save"
                      }
                    ];

            return A;        
         
    };



    return (
        <div>
            {loading ? (
              "Loading"
            ) : (
                <Menu
                    title="Menu right click"
                    style={{ top: 10, left: 10  }}
                    onClick={my_onClick_function}
                    items={ build_ITEMS_ContextMenu }
                />
            )}
        </div>
    )
    

}

export default ContextMenuDialog;
    

Just consider that I cannot simply past the code of const A directly inside the "items" element; if I do it,the code works properly. In the real life I need to build the const A with a my algorithm. This code doesn't work, the context menù is not shown!

How can I solve this problem?

the problem is that you are not calling the function. try this items={ build_ITEMS_ContextMenu() }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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