admin管理员组文章数量:1389925
I've seen a lot of threads about calling multiple traditionally declared functions in React onclick, but I'm confused how I could implement that with arrow functions. I have a function called handleClose, which closes a Material UI menu:
const [open, setOpen] = React.useState(false);
const anchorRef = React.useRef(null);
const handleClose = (event) => {
if (anchorRef.current && anchorRef.current.contains(event.target)) {
return;
}
setOpen(false);
};
And I have a function called handleModalOpen, which open a Material UI Modal:
const [modalOpen, setModalOpen] = React.useState(false);
const handleModalOpen = () => {
setModalOpen(true);
};
When I click this menu item, I would like both functions to run. Both functions individually work fine on their own. So how would I achieve this? (Currently I only have it set so the modal opens)
<MenuItem onClick={handleModalOpen}>Add Album</MenuItem>
Basically I have a button that triggers a menu, and then clicking one of those menu options should trigger a modal whilst closing the menu. Also, this is a functional ponent.
I've seen a lot of threads about calling multiple traditionally declared functions in React onclick, but I'm confused how I could implement that with arrow functions. I have a function called handleClose, which closes a Material UI menu:
const [open, setOpen] = React.useState(false);
const anchorRef = React.useRef(null);
const handleClose = (event) => {
if (anchorRef.current && anchorRef.current.contains(event.target)) {
return;
}
setOpen(false);
};
And I have a function called handleModalOpen, which open a Material UI Modal:
const [modalOpen, setModalOpen] = React.useState(false);
const handleModalOpen = () => {
setModalOpen(true);
};
When I click this menu item, I would like both functions to run. Both functions individually work fine on their own. So how would I achieve this? (Currently I only have it set so the modal opens)
<MenuItem onClick={handleModalOpen}>Add Album</MenuItem>
Basically I have a button that triggers a menu, and then clicking one of those menu options should trigger a modal whilst closing the menu. Also, this is a functional ponent.
Share Improve this question edited Jun 28, 2020 at 19:55 Evan Hsueh asked Jun 28, 2020 at 19:42 Evan HsuehEvan Hsueh 1491 gold badge3 silver badges10 bronze badges 1- 2 Does this answer your question? Call multiple functions onClick ReactJS – Emile Bergeron Commented Jun 28, 2020 at 19:46
1 Answer
Reset to default 6Either you can create an inline arrow function which calls the both
<MenuItem onClick={(e) => {handleModalOpen(); handleClose(e); }}>Add Album</MenuItem>
Or you can create a function outside and pass its reference
handleClick = (e) => {
handleModalOpen();
handleClose(e);
}
<MenuItem onClick={handleClick}>Add Album</MenuItem>
本文标签: javascriptHow to call two arrow functions onclick ReactJSStack Overflow
版权声明:本文标题:javascript - How to call two arrow functions onclick ReactJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744583308a2614052.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论