npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reconciled

v2.0.0

Published

Simple way to build a custom React renderer

Downloads

7

Readme

reconciled Build Status

Simple way to build a custom React renderer

Install

$ npm install reconciled

Usage

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();

Edit reconciled-demo

What is this for?

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.

API

reconciled(config)

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).

config

Type: object

Methods for manipulating nodes.

createNode(type, props)

Create a new node.

type

Type: string

Name of the node that's being rendered. When rendering <h1>, type equals 'h1'.

props

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:

createTextNode(text)

Create a new text node. For example, when rendering <h1>Hello World</h1>, a text node will be created where text equals 'Hello World'.

text

Type: string

Value of text node.

Example implementation:

const createTextNode = text => document.createTextNode(text);

Learn more:

setTextNodeValue(node, text)

Update value of a text node.

node

Type: any

Text node to update.

text

Type: string

Text to update in a text node.

Example implementation:

const setTextNodeValue = (node, text) => {
	node.textContent = text;
};

Learn more:

appendNode(parentNode, childNode)

Insert a new node to parent node as the last child.

parentNode

Type: any

Parent node to append child node into.

childNode

Type: any

Child node to append.

Example implementation:

const appendNode = (parentNode, childNode) => {
	parentNode.appendChild(childNode);
};

Learn more:

insertBeforeNode(parentNode, newChildNode, beforeChildNode)

Insert a new node before a certain child node.

parentNode

Type: any

Parent node to insert child node into.

newChildNode

Type: any

Node to insert.

beforeChildNode

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:

updateNode(node, oldProps, newProps)

Update node with new props.

node

Type: any

Node to update.

oldProps

Type: object

Set of old props. Compare newProps to this object to figure out which props were removed.

newProps

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:

removeNode(parentNode, childNode)

Remove child node from parent node.

parentNode

Type: any

Parent node.

childNode

Type: any

Child node.

Example implementation:

const removeNode = (parentNode, childNode) => {
	parentNode.removeChild(childNode);
};

Learn more:

render()

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.

reconciler

Type: object

render(node)

Update current node tree with a new node and its children.

node

Type: JSX.Element

New node to replace the current one with.

Example:

reconciler.render(<h1>Hello Mind Flayer</h1>);

unmount()

Unmount entire node tree and remove all nodes.