Git Product home page Git Product logo

reconciled's Introduction

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.

reconciled's People

Stargazers

Franz Fletcher avatar qintao avatar Oskar Löfgren avatar Mohit Kumar Toshniwal avatar Kacper Bąk avatar Lloyd Zhou avatar  avatar Jitendra Nirnejak avatar  avatar yujonglee avatar Bulat Shakirzyanov avatar Krzysztof Saczuk avatar Simon Lagos avatar Tenvi avatar Cat  avatar Alon Valadji avatar Houfeng avatar  avatar Artur avatar Anix avatar Huang Shuo-Han avatar Maxim Shammasov avatar Mitja Kramberger avatar John Grishin avatar Marvin Frachet avatar Valerio Proietti avatar Thram avatar James 'Tucker' Wray avatar Nimalan avatar Carl Barrdahl avatar Helder S Ribeiro avatar MetaSky avatar Tom avatar Matteo Hertel avatar Fredy Mendez avatar 爱可可-爱生活 avatar Jacob Beltran avatar Leechael avatar Michel avatar  avatar Dan Okkels Brendstrup avatar Yichuan Shen avatar ashinzekene avatar nth avatar Francisco Joshua avatar Brian Kim avatar Nikolas Evers avatar Tianqi Han avatar Afief Sholichuddin avatar Ifiok Jr. avatar Daniel Beutner avatar Mike Volovar avatar Wang avatar uetchy avatar Derya Cortuk avatar Athanasios Eleftheriadis avatar Alasdair Smith avatar  avatar James Benner avatar  avatar Alex avatar Dmitry Ivakhnenko avatar Ilya Lesik avatar Mike Podgorniy avatar Anton Kuznetsov avatar Mayank Jethva avatar Andy Wermke avatar Shuhel Ahmed avatar Paul Rumkin avatar Nik Jmaeff avatar Szymon Marczak avatar Nihhaar_RC avatar Nish avatar luyx2412 avatar Jidé avatar C avatar Paolo Moretti avatar ⊣˚∆˚⊢ avatar James Vickery avatar Cameron Hunter avatar Xiaoru Li avatar Radu Paraschiv avatar Jonathan Biro avatar Ivan Verevkin avatar pshu avatar Porramate Lim avatar Andrey avatar Steve Hall avatar Marwan Hilmi avatar Martin Olsansky avatar Zeljko avatar Thejdeep Gudivada avatar Blake Knight avatar Rish avatar Armando Magalhães avatar André Neves avatar Cong Yu avatar Colin Gourlay avatar Andreas Biørn-Hansen avatar Adam Ramberg avatar

Watchers

Vadim Demedes avatar James Cloos avatar Cody Averett avatar Marvin Frachet avatar Ergenekon Yiğit avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.