Understanding the codebase #4055
-
As stated on https://preactjs.com/about/project-goals/, one of the goals of this project is that "Understanding the codebase should take no more than a few hours." I am looking into the codebase of Preact to answer the following question: Where is the logic that decides when to render/diff which parts of the view? I would also like to understand the relationship between |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
The way Preact works is conceptually similar to a typical depth-first tree traversal algorithm. The first render starts of at root node (the one you pass to If you're using only the core library itself or the Now signals in comparison do know which parts they need to render and which not to. That works by adding a whole layer above Preact which stores that information. Every signal knows its subscribers and any dependents if available. When a signal is updated it walks through the dependency tree and updates them. Since signals must not have circular dependencies it's again a form of tree traversal, but this time with topological sorting. The update chain is always The Inside the Note that the phrase "Understanding the codebase should take no more than a few hours." is about the core library, not about understanding the whole ecosystem together like with signals, etc. |
Beta Was this translation helpful? Give feedback.
Yes exactly, the hooks addon attaches some callbacks to the renderer via the
option.*
interface. These allow you to plug into the renderer and be notified when twovnode
's are diffed, when they render, when a node completed diffing and other hooks. Through that the addon can keep track of the currently rendering component and gain access to itssetState
method.currentComponent
variable too. And just before we render, we create anEffect
signal and set it's update method to thatsetState
method of the currently rendering component. The signals you access don't…