I started this project out of interest in building a UI framework for a completely different language/platform inspired by React. Quickly realized I didn’t understand everything React was doing to allow their API. Rebuilding React was extremely useful for learning more about the framework. Hopefully, this article is useful to other people in the same way.
Just in case:<p>Sciter has Reactor - its own native and built-in implementation of React.<p>Main difference is that component is instanceof Element - components are DOM elements. This allows to unify React and WebComponent under the same umbrella/mechanism. Yet to extend standard DOM methods by use of JSX, so this<p><pre><code> someDomElement.append(<div>Hello</div>)
</code></pre>
works just fine - appends content from the vDOM.<p>In fact Reactor is three entities implemented natively:<p>* JSX - embedded into JS compiler (830 LOC) - <a href="https://gitlab.com/c-smile/quickjspp/-/blob/master/quickjs-jsx.h" rel="nofollow">https://gitlab.com/c-smile/quickjspp/-/blob/master/quickjs-j...</a> , produces vDOM literals<p>* Element.prototype.patch(JSX/vDOM) (420 LOC) - reconciliation / diff implementation<p>* Binding of lifecycle methods (200 LOC) - componentDidMount and Co.<p>There is also native implementation of Signal's (310 LOC) but they are optional for React'alike functionality.
Nice! I did a similar project, more focused on creating an Elm architecture framework in TypeScript here[1]). I based it on implementing virtual-dom logic for both Elm codebases and Derw's renderer. Since it's a decent amount of code, I couldn't find a good way to represent it in a blog post, and ended up making a commit-by-commit example on Github instead. Since it's FP-focused, it doesn't support useEffect/useState like this post, but rather has an external subscription/message system instead.<p>It does support server-side-rendering, hydratation, and async events. It's a bit different in implementation in the OP's post, but I think the most important thing any reader should take away is that at the core, virtual-doms can be quite simple with plenty of room for further optimization.<p>[1] - <a href="https://github.com/eeue56/make-your-own-tea/pull/1">https://github.com/eeue56/make-your-own-tea/pull/1</a>
About React.createElement, implementing a compatible version ended up being my goto helper for small UIs.<p>For example, I use it in mockaton here:<p><a href="https://github.com/ericfortis/mockaton/blob/7e6ad6226d7ed48e17897e5e66efe2fe2102ca49/src/Dashboard.js#L313">https://github.com/ericfortis/mockaton/blob/7e6ad6226d7ed48e...</a><p>So if at some point I need React I can simply delete my version of createElement