Simple way to build a custom React renderer
$ npm install reconciled
Here's a minimal version of react-dom
implemented with reconciled
without support for updates or events:
import reconciled from 'reconciled';
const reconciler = reconciled({
createNode: type => document.createElement(type),
createTextNode: text => document.createTextNode(text),
setTextNodeValue: (node, text) => (node.textContent = text),
appendNode: (parentNode, childNode) => parentNode.appendChild(childNode),
});
const app = reconciler.create(document.body);
app.render(<h1>Stranger Things</h1>);
app.unmount();
React itself doesn't require a certain environment to run. In simple terms, it only manages components and updates. Then, renderer takes representation of this component tree and displays it somewhere. For example, every React app in the browser uses react-dom renderer. react-native lets you build mobile native apps with React. Ink renders your React app in the terminal. There's even react-360 renderer for building VR apps.
Custom React renderers let you render React apps anywhere you want, as long as you can build a custom renderer for it. This is where reconciled
comes in. There's not a lot of documentation around building custom renderers, so I extracted all my knowledge of building them for Ink into a simple-to-use module. Enjoy.
I think reconciled
is a good first step in learning how to make a React renderer. If you notice that reconciled is too limited for your use case, I'd recommend checking out its source code and building your renderer without using reconciled
.
Create a reconciler with the specified config for your custom renderering. Returns a reconciler object. Reconciler is a set of methods that let you synchronize React's state with your custom output (see example above).
Type: object
Methods for manipulating nodes.
Create a new node.
Type: string
Name of the node that's being rendered. When rendering <h1>
, type
equals 'h1'
.
Type: object
Props to add to the node. When rendering <input value="abc"/>
, props
equals { value: 'abc' }
.
Example implementation:
const createNode = (type, props) => {
const node = document.createElement(type);
for (const [key, value] of Object.entries(props)) {
node.setAttribute(key, value);
}
return node;
};
Learn more:
Create a new text node. For example, when rendering <h1>Hello World</h1>
, a text node will be created where text
equals 'Hello World'
.
Type: string
Value of text node.
Example implementation:
const createTextNode = text => document.createTextNode(text);
Learn more:
Update value of a text node.
Type: any
Text node to update.
Type: string
Text to update in a text node.
Example implementation:
const setTextNodeValue = (node, text) => {
node.textContent = text;
};
Learn more:
Insert a new node to parent node as the last child.
Type: any
Parent node to append child node into.
Type: any
Child node to append.
Example implementation:
const appendNode = (parentNode, childNode) => {
parentNode.appendChild(childNode);
};
Learn more:
Insert a new node before a certain child node.
Type: any
Parent node to insert child node into.
Type: any
Node to insert.
Type: any
Node used for reference, so that new node is inserted before this one.
Example implementation:
const insertBeforeNode = (parentNode, newChildNode, beforeChildNode) => {
parentNode.insertBefore(newChildNode, beforeChildNode);
};
Learn more:
Update node with new props.
Type: any
Node to update.
Type: object
Set of old props. Compare newProps
to this object to figure out which props were removed.
Set of new props.
Example implementation:
const updateNode = (node, oldProps, newProps) => {
for (const key of Object.keys(oldProps)) {
if (newProps[key] === undefined) {
node.removeAttribute(key);
}
}
for (const [key, value] of Object.entries(newProps)) {
node.setAttribute(key, value);
}
};
Learn more:
Remove child node from parent node.
Type: any
Parent node.
Type: any
Child node.
Example implementation:
const removeNode = (parentNode, childNode) => {
parentNode.removeChild(childNode);
};
Learn more:
Callback which is called after every change is completed by the reconciler. Useful for renderers like Ink, which have to build custom output for environment that renderer is built for. In Ink's case, it's terminal, so there's no DOM like in the browser. Use this callback as an indication that there has been a change and UI needs to be updated.
Type: object
Update current node tree with a new node and its children.
Type: JSX.Element
New node to replace the current one with.
Example:
reconciler.render(<h1>Hello Mind Flayer</h1>);
Unmount entire node tree and remove all nodes.