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.

Question: Does Preact re-render the whole vdom when a components state changes?

See original GitHub issue

Hello!

I have a bit off-topic question regarding preact internals:

I’m doing a small hobby project in which i am creating a vdom library that can render components(like preact but super simplified) and i’m wondering how react handles the vdom diffing when a component changes state.

Lets say i have this structure:

document.getElementById('root').appendChild(
  <div>
    Some text
    <span>
      Some text
      <SomeComponent />
    </span>
    <p>More text</p>
  </div>
)

Lets now say that SomeComponent has a state change and has to re-render. Does preact patch the the whole vdom or just the SomeComponent and its children?

Currently, if anything changes anywhere, i have to diff & patch the whole vdom tree like this:

const $root = document.getElementById('root')
patch($root, newVdom, oldVdom)

Are you doing something smart to diff only a part of the vdom? I would like to do something like this when something changes in my component:

patch(component.$parentElement, newVdom, oldVdom)

Am i missing something by thinking like this? I tried to go through the source code of Preact but it was a bit too much to wrap my head around exactly how the updates works.

Again, a bit off-topic but i couldn’t help myself. I’m curious how you guys deal with it!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
developitcommented, Nov 4, 2016

Hi there! You’re right to think way - preact re-renders starting from the point where state changed. It’s a nice little optimization!

1reaction
linde12commented, Nov 4, 2016

Diffing against the real DOM makes sense. I’ll continue with this approach for now and look around in the source of preact. Closing this, thanks again. Inspiring stuff you’ve done with Preact! 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does Preact re-render the whole vdom when a components ...
Hi there! You're right to think way - preact re-renders starting from the point where state changed. It's a nice little optimization!
Read more >
How and when to force a React component to re-render
React automatically re-renders components, but what happens when a component is not updating as expected? Find out what you can do here.
Read more >
External DOM Mutations | Preact
With this lifecycle hook in place and telling Preact not to re-render the Component when changes occur up the VDOM tree, your Component...
Read more >
How does React decide to re-render a component? - Lucy Bain
A re-render can only be triggered if a component's state has changed. The state can change from a props change, or from a...
Read more >
Reconciliation - React
When a component updates, the instance stays the same, so that state is maintained across renders. React updates the props of the underlying...
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