[Accordion] Expand icon button calls onChange twice per click
See original GitHub issueThe expand icon button calls the onChange function twice per click.
It was tripping me up when I was trying to perform an action when the panel was expanded (and only wanted the action performed once).
Expected Behavior
Should only call onChange once (i.e. when the state of the panel changes).
Current Behavior
Calls it twice per state change (ONLY when the expanded button is clicked - it calls only once when the summary section is clicked, which is inconsistent). Upsets things when trying to perform an action based on a state change (ends up doing it twice). Can be worked around by maintaining a local instance variable and comparing with the provided expanded parameter.
onExpandChange = (event, expanded) => {
if (expanded && expanded != this.prevExpanded) {
// Call server and do other stuff
}
this.prevExpanded = expanded;
}
Steps to Reproduce (for bugs)
Example of behavior (to show double call):
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
[Accordion] Expand icon button calls onChange twice per click
Calls it twice per state change (ONLY when the expanded button is clicked - it calls only once when the summary section is...
Read more >Material-UI: Expand Accordion by clicking the icon only
1- Create our own state with handler: const [expand, setExpand] = React.useState(false); const toggleAcordion = () => { setExpand((prev) => !
Read more >The Best MUI Accordion onClick, Expand, and Icons Tutorial
The Accordion has an onChange event that fires when the component is expanded or contracted. The expansion and contraction is handled internally ...
Read more >Menu - Ant Design
A versatile menu for navigation. ... Ant Design offers two navigation options: top and side. ... expandIcon, custom expand icon of submenu ...
Read more >React Transition component - Material UI - MUI
Expand from the start edge of the child element. Use the orientation prop if you need a horizontal collapse. The collapsedSize prop can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I will try to make some time for this issue tonight.
The issue comes from the fact that we listen twice to the onClick event. I don’t think that it’s needed. Worse, it’s introducing an unexpected behavior. We can simply remove this line: https://github.com/mui-org/material-ui/blob/fd7facc447dea934704be92d208e6268a48c9d02/src/ExpansionPanel/ExpansionPanelSummary.js#L137 And rely on event bubbling.