trueadm / inferno
- понедельник, 6 июня 2016 г. в 03:12:13
JavaScript
An extremely fast, React-like JavaScript library for building modern user interfaces
Inferno is an isomorphic library for building high-performance user interfaces, which is crucial when targeting mobile devices. Unlike typical virtual DOM libraries like React, Mithril, Cycle and Om, Inferno uses intelligent techniques to separate static and dynamic content. This allows Inferno to only "diff" renders that have dynamic values.
In addition to this, we've painstakingly optimized the code to ensure there is as little overhead as possible. We believe that Inferno is currently the fastest virtual DOM implementation out there - as shown by some of our benchmarks. Inferno is all about performance, whilst keeping a robust API that replicates the best features from libraries such as React.
In principle, Inferno is compatible with the standard React API, allowing painless transition from React to Inferno. Furthermore, Inferno has a Babel plugin allowing JSX syntax to transpile to optimised Inferno virtual DOM.
inferno-component
inferno-server
Very much like React, Inferno requires the inferno
and the inferno-dom
packages for consumption in the browser's DOM. Inferno also has the inferno-server
package for
server-side rendering of virtual DOM to HTML strings (differing from React's route of using react-dom/server
for server-side rendering). Furthermore, rather than include the
ES2015 component with class syntax in core (like React), the component is in a separate package inferno-component
to allow for better modularity.
NPM:
Core package:
npm install --save inferno
ES2015 stateful components (with lifecycle events) package:
npm install --save inferno-component
Browser DOM rendering package:
npm install --save inferno-dom
Helper for creating Inferno VNodes (similar to React.createElement
):
npm install --save inferno-create-element
Server-side rendering package:
npm install --save inferno-server
Pre-bundled files for browser consumption:
http://infernojs.org/releases/0.7.8/inferno.min.js
http://infernojs.org/releases/0.7.8/inferno-create-element.min.js
http://infernojs.org/releases/0.7.8/inferno-component.min.js
http://infernojs.org/releases/0.7.8/inferno-dom.min.js
http://infernojs.org/releases/0.7.8/inferno-server.min.js
Let's start with some code. As you can see, Inferno intentionally keeps the same, good, design ideas as React regarding components: one-way data flow and separation of concerns. In these examples, JSX is used via the Inferno JSX Babel Plugin to provide a simple way to express Inferno virtual DOM.
import Inferno from 'inferno';
import InfernoDOM from 'inferno-dom';
const message = "Hello world";
InfernoDOM.render(
<MyComponent message={ message } />,
document.getElementById("app")
)
Furthermore, Inferno also uses ES6 components like React:
import Inferno from 'inferno';
import { Component } from `inferno-component`;
import InfernoDOM from 'inferno-dom';
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
counter: 0
}
}
render() {
return (
<div>
<h1>Header!</h1>
<span>Counter is at: { this.state.counter }</span>
</div>
)
}
}
InfernoDOM.render(<MyComponent />, document.body);
The real difference between React and Inferno is the performance offered at run-time. Inferno can handle large, complex DOM models without breaking a sweat. This is essential for low-powered devices such as tablets and phones, where users are quickly demanding desktop-like performance on their slower hardware.
Creates an Inferno VNode object that has chainable setting methods.
import createVNode from `inferno`;
InfernoDOM.render(createVNode().setTag('div').setClassName('foo').setAttrs({ id: 'test' }).setChildren('Hello world!'), document.body);
Creates an Inferno VNode using a predefined blueprint. Using the reference to the blueprint, it allows for faster optimisations with little overhead.
import InfernoDOM from 'inferno-dom';
const myBlueprint = Inferno.createBlueprint({
tag: 'div',
attrs: {
id: 'foo'
},
children: { arg: 0 }
});
InfernoDOM.render(myBlueprint('foo'), document.body);
For each property on the object passed as the argument to createBlueprint
, anything that has been defined with { arg: X }
is regarded as a dynamic value (matching the argument of calling this blueprint), otherwise the properties are regarded as static.
For example: if my object is const blueprint = Inferno.createBlueprint({ tag: { arg: 0 } })
, then you'd expect to call blueprint('div')
with the argument 0
(first argument) being the tag for the VNode.
Creates an Inferno VNode using a similar API to that found with React's createElement
import InfernoDOM from 'inferno-dom';
import Component from 'inferno-component';
import createElement from 'inferno-create-element';
class BasicComponent extends Component {
render() {
return createElement('div', {
className: 'basic'
},
createElement('span', {
className: this.props.name
}, 'The title is ', this.props.title)
)
}
}
InfernoDOM.render(createElement(BasicComponent, { title: 'abc' }), document.body);
Stateful component:
import Component from 'inferno-component';
class MyComponent extends Component {
render() {
...
}
}
This is the base class for Inferno Components when they're defined using ES6 classes.
Stateless component:
const MyComponent => ({ name, age }) =>
<span>My name is: { name } and my age is: {age}</span>
);
Stateless components are first-class functions where their first argument is the props
passed through from their parent.
import InfernoDOM from 'inferno-dom';
InfernoDOM.render(<div />, document.body);
Render a virtual node into the DOM in the supplied container given the supplied virtual DOM. If the virtual node was previously rendered into the container, this will perform an update on it and only mutate the DOM as necessary, to reflect the latest Inferno virtual node.
import InfernoServer from 'inferno-server';
InfernoServer.renderToString(<div />, document.body);
Render a virtual node into an HTML string, given the supplied virtual DOM.
Please note: hooks are provided by inferno-dom
;
Inferno supports many of the basic events on DOM nodes, such as onClick
, onMouseOver
and onTouchStart
. Furthermore, Inferno allows you to attach
common hooks directly onto components and DOM nodes. Below is the table of all possible hooks available in inferno-dom
.
Name | Triggered when | Arguments to callback |
---|---|---|
onCreated |
a DOM node has just been created | domNode |
onAttached |
a DOM node being attached to the document | domNode |
onWillDetach |
a DOM node is about to be removed from the document | domNode |
onWillUpdate |
a DOM node is about to perform any potential updates | domNode |
onDidUpdate |
a DOM node has performed any potential updates | domNode |
onComponentWillMount |
a stateless component is about to mount | domNode, props |
onComponentDidMount |
a stateless component has mounted successfully | domNode, props |
onComponentWillUnmount |
a stateless component is about to be unmounted | domNode, props |
onComponentShouldUpdate |
a stateless component has been triggered to updated | domNode, lastProps, nextProps |
onComponentWillUpdate |
a stateless component is about to perform an update | domNode, lastProps, nextProps |
onComponentDidUpdate |
a stateless component has performed an updated | domNode, props |
It's simple to implicitly assign hooks to both DOM nodes and stateless components.
Please note: stateful components (ES2015 classes) from inferno-component
do not support hooks.
function createdCallback(domNode, props) {
// [domNode] will be available for DOM nodes and components (if the component has mounted to the DOM)
// [props] will only be passed for stateless components
}
InfernoDOM.render(<div onCreated={ createdCallback } />, document.body);
function StatelessComponent({ props }) {
return <div>Hello world</div>;
}
InfernoDOM.render(<StatelessComponent onComponentWillMount={ createdCallback } />, document.body);
Hooks provide powerful lifecycle events to stateless components, allowing you to build components without being forced to use ES2015 classes.
Inferno tries to address two problems with creating UI components:
Writing code should be fun. Browsers are getting more advanced and the technologies being supported are growing by the week. It's about time a framework offered more fun without compromising performance.
Inferno has its own JSX Babel plugin.
Inferno strives to be compatible with much of React's basic API. However, in some places, alternative implementations have been used. Non-performant features have been removed or replaced where an alternative solution is easy to adopt without too many changes.
Inferno wants to always deliver great performance and in order to do so, it has to make intelligent assumptions about the state of the DOM and the elements available to mutate. Custom namespaces conflict with this idea and change the schema of how different elements and attributes might work; so Inferno makes no attempt to support namespaces. Instead, SVG namespaces are automatically applied to elements and attributes based on their tag name
.
React's ES2015 component is referenced as React.Component
. To reduce the bloat on the core of Inferno
, we've extracted the ES2015 component
into its own package, specifically inferno-component
rather than Inferno.Component
. Many users are opting to use stateless components with
Inferno's hooks
to give similar functionality as that provided by ES2015 components.
Inferno makes no attempt to add the unit to numerical attributes or properties that React attempts to automatically add units to. For example:
<div style={ { left: 10 } }/>
will result in px
being added automatically to the style property in React. To ensure Inferno is kept lean and fast, the
code base does not contain these expensive checks and overheads have been removed. It's completely down to the user to specify the property.
So with Inferno, you should use the following to achieve the same result <div style={ { left: '10px' } } />
.
npm run test:browser // browser tests
npm run test:server // node tests
npm run test // browser and node tests
npm run browser // hot-loaded browser tests
npm run build
npm run lint:source // lint the source