[英]React Creating Menu items by a function
I have a menu with list items like below 我有一个包含以下列表项的菜单
<MenuItem
//className={this.classes.menuItem}
onClick={this.handleClose}
>
<NavLink
to={Constants.pagesURL+page}
//className={this.classes.menuItemAnchor}
>
{Constants.pagesName+page}
</NavLink>
</MenuItem>
I want to wrap this with menuItem(page)
function so within the page, all I need to do is {this.menuItem('Home')} and {this.menuItem('Page1')}
to get menu items populated, easy and clean.. 我想用menuItem(page)
函数包装它,所以在页面内,我要做的就是{this.menuItem('Home')} and {this.menuItem('Page1')}
以方便地填充菜单项干净..
The struggle is I want the name I send when calling the function to be added to the Constant name.. For example, if I do {this.menuItem('Home')}
then {Constants.pagesName+page}
needs to be actually {Constants.pagesNameHome}
.. 困难在于我希望将调用函数时发送的名称添加到常量名称中。例如,如果我执行{this.menuItem('Home')}
则实际上需要{Constants.pagesName+page}
{Constants.pagesNameHome}
..
I tried as above with adding + in front of page
I send in, not working.. I tried {Constants.pagesName[page]}
not working, I tried to create let pageName = 'pagesName'+page;
我如上所述尝试在发送的page
前添加+,但不起作用。.我尝试了{Constants.pagesName[page]}
不起作用,我试图创建let pageName = 'pagesName'+page;
then doing {Constants.pagesName}
that didn't work either. 然后执行{Constants.pagesName}
也不起作用。 How can I get this work? 我如何获得这项工作?
As it is {Constants.pagesURL+page}
I get 'undefinedHome' and 'undefinedPage1'... 原来是{Constants.pagesURL+page}
我得到了'undefinedHome'和'undefinedPage1'...
你试过了吗:
const menuItem = (page) => `${Constants.pagesURL}${page}`;
Okay this was rather easy as I expected.. Playing around, I figured this is the way to go.. 好的,这很容易实现,就像我预期的那样。.玩转,我认为这是要走的路。
{Constants['pagesURL'+page]}
Works as a charm! 发挥魅力!
Full code: 完整代码:
menuItem(page) {
return(
<MenuItem
//className={this.classes.menuItem}
onClick={this.handleClose}
>
<NavLink
to={Constants['pagesURL'+page]}
//className={this.classes.menuItemAnchor}
>
{Constants['pagesName'+page]}
</NavLink>
</MenuItem>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.