英文:
How do we add Multiple onclick event in react.js
问题
以下是您要翻译的内容的翻译部分:
"I came across one scenario that the onclick button is already been used but at the same time i need to add one more event to to add history.push(), I was working on sidebar component using material UI so need to customize few things I added the code
const menuItems = [
{
text: 'Home',
icon: HomeIcon,
onClick: () => history.push('/'),
},
{
text: 'Log In',
icon: MailIcon,
onClick: () => history.push('/login'),
},
{
text: 'Sign Up',
icon: HomeIcon,
onClick: () => history.push('/signup'),
},
];
...
{menuItems.map(({ text, icon: Icon, onClick}, index) => (
))}
I tried something Like this
const menuItems = [
{
text: 'Home',
icon: HomeIcon,
to: "/",
},
{
text: 'Log In',
icon: MailIcon,
to:"/login",
},
"
如果您有其他需要翻译的部分,请告诉我。
英文:
I came across one scenario that the onclick button is already been used
but at the same time i need to add one more event to to add history.push(),
I was working on sidebar component using material UI
so need to customize few things I added the code
const menuItems = [
{
text: 'Home',
icon: HomeIcon,
onClick: () => history.push("/"),
},
{
text: 'Log In',
icon: MailIcon,
onClick: () => history.push("/login"),
},
{
text: 'Sign Up',
icon: HomeIcon,
onClick: () => history.push("/signup"),
},
];
...
<List>
{menuItems.map(({ text, icon: Icon, onClick}, index) => (
<ListItem
button
onClick={
item === menus[2]
? handleSubMenu1Click
: handleSubMenu2Click
}
>
<ListItemIcon>
<Icon />
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
I tried something Like this
const menuItems = [
{
text: 'Home',
icon: HomeIcon,
to: "/",
},
{
text: 'Log In',
icon: MailIcon,
to:"/login",
},
<ListItem element={Link} to={item.to} button key={text}>
If we have submenu and also need to navigate across what would be the best Approach
答案1
得分: 1
使用事件冒泡将单个 onClick
处理程序附加到您的 <List>
组件,而不是为每个 <ListItem>
附加多个处理程序,详见此处:https://blog.logrocket.com/event-bubbling-capturing-react/。
在您的处理函数中,您将获得一个包含指向所点击项目的引用的 SyntheticEvent
对象,您可以使用它来确定接下来要采取的操作。
请参阅此沙盒: https://codesandbox.io/s/snowy-sea-998zvl?file=/src/App.js
然而,从您的代码中我推断您只是想要创建一个带有链接的导航菜单,为此,您可能只想使用 react-router-dom
的 <Link>
组件,如此示例所示:https://codesandbox.io/s/exciting-diffie-putb4f?file=/src/Nav.js
英文:
Use Event Bubbling to attach a single onClick
handler to your <List>
component instead of attaching multiple handlers to each <ListItem>
as described here https://blog.logrocket.com/event-bubbling-capturing-react/.
In your handler function, you'll get a SyntheticEvent
object whose target
property will contain a reference to the clicked item, which you can use to determine what actions to take next.
See this sandbox: https://codesandbox.io/s/snowy-sea-998zvl?file=/src/App.js
However, from your code I infer you're just trying to create a navigation menu with links, and for that, you may just want to use react-router-dom
's <Link>
component instead as shown in this sandbox: https://codesandbox.io/s/exciting-diffie-putb4f?file=/src/Nav.js
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论