Vite + Preact: useState returns true on first call
See original GitHub issueReproduction
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
- run
yarn
at the root. - run
yarn dev
. - See console logs (should output “true” on First calls).
Expected Behavior
- Should have similar output with ‘Next Call’ logs.
setState
should be able to receive the current state.
Actual Behavior
- Initial call for a
useState
returns ‘true’ instead of the state and the setState function.
- adding a set state action function as a parameter for a
setState
call receives an undefined state.
Additional info
preact@^10.4.8
vite@^1.0.0-rc.4
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (5 by maintainers)
Top 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 >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 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?
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.