Preact-router and setState callback does not play nice
See original GitHub issue"preact": "^10.4.4",
"preact-router": "^3.2.1",
Reproduction
Here is a minimal code to reproduce this issue. https://codesandbox.io/s/compassionate-rgb-kl85q?file=/src/index.js
I am setting state with this.setState
and reading it immediately in setState
callback (second argument). See line # 13
Steps to reproduce
- open console tab
- find console.log output from line # 14
Expected Behavior
state should be { initial: true, handleRoute: true }
Actual Behavior
state is { initial: true }
I can set state and read it immediately in setState
callback in componentDidMount
and other places but not in preact-router
’s onChange callback.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
When to use React setState callback - Stack Overflow
First use case is probably not a good idea. setState callbacks trigger after re-render, so you're causing a double render for no good...
Read more >Upgrading from Preact 8.x
This document is intended to guide you through upgrading an existing Preact 8.x application to Preact X and is divided in 3 main...
Read more >Preact Authentication Tutorial - Auth0
Preact is a fast 3kb React alternative with the same ES6 API. Learn how to build fast apps with Preact and add authentication...
Read more >Preact - Releases
One seemingly minor change to our jsx constructor function lead to pretty weird and nasty bug depending on whether the code was run...
Read more >preact - npm
Fast 3kb React-compatible Virtual DOM library.. Latest version: 10.11.2, last published: 9 days ago. Start using preact in your project by running `npm...
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
Thanks @JoviDeCroock for replying on Twitter 😃 From what I understood adding
c.state = c._nextState;
just before here: https://github.com/preactjs/preact/blob/0f8c55c6cfad8d5cc3aec6785c1f6940998b4782/src/diff/index.js#L251 This will render the following to the console (with the code in prev. comment):So I think this fixes the issue 🤦 🎉haha Thanks Jovi! Although it’s still not aligned with React? Is that okay?
Putting it behind render makes the intent clear of being done inside of
render
, so I’m all for it