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.

createContext is not fully React compatible

See original GitHub issue

After migrating an app from React to Preact 10, we noticed that some of our components were unmounting with the incorrect props.

Reproduction

Here is a reproduction of the issue on simplified codebase.

Preact: https://codesandbox.io/s/upbeat-pasteur-r7soh React: https://codesandbox.io/s/wonderful-worker-u3uus

Steps to reproduce

So, the idea is that because of the key prop assigned to Parent, when id changes, it should unmount it with the previous id and then mount it with the new one. That’s my expectation at least and it works like that in React. Here is the console output when clicking on the text.

// React
render, id 0
mount, id 0
render, id 1
unmount, id 0
mount, id 1

But for Preact, I’m not fully sure, but it seems that nested consumers will update first, meaning that Child will be updated with the new id and only then will Parent realize that its key has changed and will unmount. Here are the console logs for the same click:

// Preact
render, id 0
mount, id 0
render, id 1
updated from 0 to 1
render, id 1
unmount, id 1
mount, id 1

As we can see, there is an update lifecycle now and the unmount is done with props.id at 1 rather than 0.

I wonder if either React is maintaining a more optimized stack for consumers or, even better, batch all updates to all consumers in one render.

Expected Behavior

Child component should not update and unmount directly with id: 0.

Actual Behavior

Child component is updated to id: 1 and unmounted with that value.

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
pauldijoucommented, Mar 6, 2020

That was so fast! Thanks all for fixing that, you rock.

3reactions
JoviDeCroockcommented, Mar 5, 2020

Hey @pauldijou

This is still a problem with 2 vs 1 pass but specifically for context I found a solution, PR’ing it right now! I can’t begin to thank you enough for this awesome reproduction! Thanks you for making Preact better!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Context - React
Context provides a way to pass data through the component tree without having to pass props down manually at every level. In a...
Read more >
React Native - TypeError: n.createContext is not a function. (In ...
CreateContext looks like a function on application level, so it's perhaps related to the viewport or some basic app. But I'm neither working ......
Read more >
How to use React Context with TypeScript - Felix Gerschau
Learn how to use React Context and how to make it TypeScript-compatible.
Read more >
React Context for Beginners – The Complete Guide (2021)
In this comprehensive guide, we will cover what React context is, how to use it, when and when not to use context, and...
Read more >
Rendering: Server and Client Components - Next.js beta docs
import { createContext } from 'react'; // createContext is not supported in Server Components const ThemeContext = createContext(); export default ...
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