如何在react.js中添加多个onclick事件

huangapple go评论66阅读模式
英文:

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 &lt;List&gt; component instead of attaching multiple handlers to each &lt;ListItem&gt; 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 &lt;Link&gt; component instead as shown in this sandbox: https://codesandbox.io/s/exciting-diffie-putb4f?file=/src/Nav.js

huangapple
  • 本文由 发表于 2023年4月11日 00:30:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定