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.

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:closed
  • Created 3 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
jamesb3llcommented, May 27, 2020

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):

rendered: true
callback { initial: true, rendered: true }
rendered: true
mounted { initial: true, rendered: true, didMount: true }

So I think this fixes the issue 🤦 🎉haha Thanks Jovi! Although it’s still not aligned with React? Is that okay?

2reactions
JoviDeCroockcommented, May 29, 2020

Putting it behind render makes the intent clear of being done inside of render, so I’m all for it

Read more comments on GitHub >

github_iconTop 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 >

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