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.

If the component is open while it's being triggered through a state change the component stops functioning

See original GitHub issue

Describe the bug When opening the Collapsible component via a mouse click and then triggering the component to open/close via a state change of the parent component by passing through a value via the open API the component stop functioning all together.

To Reproduce

  1. Wait for the app to load
  2. Click on Accordion #1 (doesn’t really matter which one)
  3. Click on the checkbox above to open all Collapsible components on the page 4a. Click again on the checkbox and nothing will happen to Accordion #1 4b. Click again on Accordion #1 and nothing will happen
  4. The component has stopped working

Here’s a sandbox already setup with the https://codesandbox.io/s/dark-star-t2g3ou?file=/src/App.js

Expected behavior The component should open/close normally

Authors notes If this is somehow my fault for not fully understanding reacts hooks lifecycle or in general my mistake, but I’m still new to react

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

2reactions
karltaylorcommented, Mar 24, 2022

After a quick look it looks to be that this line of code is the culprit

    if (
      (prevState.height === 'auto' || prevState.height === 0) && // here.
      this.state.shouldSwitchAutoOnNextCycle === true
    ) {
      window.clearTimeout(this.timeout);
      this.timeout = window.setTimeout(() => {
        // Set small timeout to ensure a true re-render
        this.setState({
          height: 0,
          overflow: 'hidden',
          isClosed: true,
          shouldSwitchAutoOnNextCycle: false,
        });
      }, 50);
    }

I’ve removed and it seems to be working so if you want a quick fix you can fork the repo and edit and use that fork or use something like patch-package.

I’m just wanting to do a few more tests before deploying.

Best

1reaction
karltaylorcommented, Mar 24, 2022

Edited: to remove the mentions of other issues as I believe you didn’t mean to do that but was just using a # which will reference an issue/pr.

Confirmed as bug 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

When does React re-render components?
Changing the state means that React triggers an update when we call the setState function (in React hooks, you would use useState )....
Read more >
Why React doesn't update state immediately
When developing React applications, you may have noticed that state updates don't immediately reflect new values after being changed.
Read more >
Using the Effect Hook
They let you use state and other React features without writing a class. The Effect Hook lets you perform side effects in function...
Read more >
React: Stop checking if your component is mounted
This article will provide solutions to the “Can't perform a React state update on an unmounted component” warning and why a call to...
Read more >
React setState usage and gotchas
setState () enqueues changes to the component state and tells React that this component and its children need to be re-rendered with the...
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