question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Accordion] Expand icon button calls onChange twice per click

See original GitHub issue

The 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):

CodeSandbox Example Link

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
kgregorycommented, Dec 17, 2017

I will try to make some time for this issue tonight.

1reaction
oliviertassinaricommented, Dec 16, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found