preactjs / preact

⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.

Home Page:https://preactjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with rendering child component on props changes

coder4u opened this issue · comments

Describe the bug
There was a problem with the rendering of components when updating props.
If you pass the state of a parent component as a prop to a child component, and then update the state in the parent component, the child component is not rerendered.

To Reproduce
Preact - https://codepen.io/coder4u/pen/YzgdQEL

React without rerender issue - https://codepen.io/coder4u/pen/qBvLXOy

Steps to reproduce the behavior:

  1. Go to pen.
  2. Open console.
  3. You will see that parent state not synced with child props in render methods.

Expected behavior
Child component must rerender when it props changed.

Looks like this got broken between 10.18.1 and 10.18.2, wondering whether this contributed to it. Will investigate.

EDIT: this looks to be a timing issue as it's not really reproducible in our test suite 😅

Did some debugging, maybe it could help.

When BrokenChild is rendered for the first time its vnode._original == 3 (++vnodeId).
After setState in BrokenChild.componentDidUpdate it increments to 4. Note that global vnodeId == 3 at this time.
When App component re-renders after setTimeout it creates a new vnode for BrokenChild with _original == 4 (++vnodeId).
Then rendering bails out because oldVNode._original == newVNode._original.

Before the mentioned change _original was changed on a temporary copy of the oldVNode but now it is changed on the newVNode.

Yeah, I got to a similar conclusion, we might just need to change the vnodeId back after diffing 😅