Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State update issue in Preact 10.10.3 #3674

Closed
robertknight opened this issue Aug 17, 2022 · 0 comments · Fixed by #3675
Closed

State update issue in Preact 10.10.3 #3674

robertknight opened this issue Aug 17, 2022 · 0 comments · Fixed by #3675

Comments

@robertknight
Copy link
Member

robertknight commented Aug 17, 2022

Describe the bug

I encountered a scenario in Preact 10.10.3 where a state update does not work after the initial render. The test case below works in earlier Preact releases (I tested 10.10.0-10.10.2).

Test case:

import { h, render } from "preact";
import { useState } from "preact/hooks";

function Widget() {
  const [, setUnusedState] = useState(0);
  const [suggestionsListOpen, setSuggestionsListOpen] = useState(false);

  const open = () => {
    // Test works if line below is commented out
    setUnusedState(42);
    setSuggestionsListOpen(true);
  };

  const close = () => {
    setSuggestionsListOpen(false);
  };

  return (
    <div>
      <button onClick={open}>Open</button>
      <button onClick={close}>Close</button>
      <div data-testid="state">{suggestionsListOpen ? "open" : "closed"}</div>
    </div>
  );
}

const root = document.querySelector("#app");
render(<Widget />, root);

Steps to reproduce

  1. Open the sandbox at https://codesandbox.io/s/preact-10-10-3-state-updates-7xs51b, which uses the test case above
  2. Click on "Open".
  3. Click on "Close"

Expected behavior: The text below the button should change to "open" after clicking the "Open" button and change to "closed" after clicking the "Close" button.

Actual behavior: The text changes to "Open" after clicking "Open" but does not update after clicking "Close".

After clicking the "Open" button a second time, the Open and Close buttons then behave as expected. The issue has something to do with the sequence of states that are updated. See the note above the code which calls setUnusedState.

In Preact 10.10.2 and earlier, the example works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant