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.

Vite + Preact: useState returns true on first call

See original GitHub issue

Reproduction

Link to example: https://github.com/LXSMNSYC/scoped-model/tree/master/examples/preact-scoped-model

Sandbox: https://codesandbox.io/s/musing-spence-45g0y

  console.log('First call', useState(0));
  const [count, setCount] = useState(initialCount ?? 0);
  console.log('Next call', useState(0));

  const increment = useCallback(() => {
    setCount((c) => {
      console.log('Current State', c);
      return c + 1;
    });
  }, []);

  const decrement = useCallback(() => {
    setCount((c) => {
      console.log('Current State', c);
      return c - 1;
    });
  }, []);

Steps to reproduce

  1. run yarn at the root.
  2. run yarn dev.
  3. See console logs (should output “true” on First calls).

Expected Behavior

  1. Should have similar output with ‘Next Call’ logs.
  2. setState should be able to receive the current state.

Actual Behavior

  1. Initial call for a useState returns ‘true’ instead of the state and the setState function.
Screen Shot 2020-09-04 at 6 48 13 PM
  1. adding a set state action function as a parameter for a setState call receives an undefined state. Screen Shot 2020-09-04 at 6 49 06 PM

Additional info

  • preact@^10.4.8
  • vite@^1.0.0-rc.4

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
JoviDeCroockcommented, Sep 9, 2020

I don’t think this is our fault, it works in other bundlers. Essentially we could implement something that prevents double injection but the code is still included duplicated which is never a good thing.

We are relying on default resolution behaviour where it deduplicates.

A reason for this error however could be a lot of mixed imports within this lib code itself and the bundler used to produce the dist.

Maybe scoped-model should limit itself to only Preact and Preact/hooks imports for a Preact specific version?

1reaction
lxsmnsyccommented, Sep 9, 2020

Thanks for the response @JoviDeCroock.

I’ll close this for now. I’ll be trying out other JS bundlers and see if the problem still persists.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useState set call not reflecting change immediately prior to first ...
Based on results within the the validIds array, I check to see if it's true or false and set authorised state to true...
Read more >
useState in React: A complete guide - LogRocket Blog
React finds a call to useState , creates a new Hook object (with the initial state), changes the current Hook variable to point...
Read more >
A Complete Beginner's Guide to React useState hook [Part 1]
The useState hook takes an initial state, count in this case, and returns a pair of variables, count and setCount , where count...
Read more >
What is useState() in React ? - GeeksforGeeks
The useState hook is a special function that takes the initial state as an argument and returns an array of two entries.
Read more >
preact/hooks useEffect JavaScript Examples
This page shows JavaScript code examples of preact/hooks useEffect.
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