Inferno
repository·master·Indexed 12 days ago
https://github.com/infernojs/infernoAn extremely high-performance, React-like library for building user interfaces, optimized for real-time data and large DOM trees. The ecosystem includes inferno-animation for CSS animations, inferno-compat for React compatibility, inferno-clone-vnode for virtual node cloning, and inferno-create-element for element creation.
What's inside Inferno
- Inferno is a high-performance, React-like library designed for building fast user interfaces on both the client and server. It focuses on providing the fastest possible runtime performance, making it ideal for applications rendering real-time data views or large DOM trees. It uses a component-driven architecture with one-way data flow and supports both class and functional components.
What is inferno-compat and when to use it
masterinferno-compatis a compatibility layer designed to make React-based modules work with Inferno without requiring code changes. It provides the same exports asreactandreact-dom, allowing you to use build tool aliasing to swap React for Inferno.Important Considerations:
- Performance: There is an associated overhead; you should not expect native Inferno performance when using this layer.
- Scope: You might not always need the full
inferno-compatpackage; sometimes a simple alias toinfernois sufficient. If you need specific features likecreateElementsupport, you must also install the corresponding package (e.g.,inferno-create-element).
Define Class and Functional Components
masterInferno supports two primary ways to define components:
- Class Components: Extend the
Componentclass frominfernoand implement arender()method. - Functional Components: Standard JavaScript functions where the first argument is
props.
Functional components can also use
defaultPropsanddefaultHooks(e.g.,onComponentShouldUpdate).// Class Component import { Component } from 'inferno'; class MyComponent extends Component { render() { return <div>My Component</div> } } // Functional Component const MyComponent = ({ name, age }) => ( <span>My name is: { name } and my age is: {age}</span> ); // Functional Component with defaultProps export function MyFunctionalComponent({value}) { return <div>{value}</div>; } MyFunctionalComponent.defaultProps = { value: 10 };- Class Components: Extend the
Use Inferno with third-party state libraries
masterInferno provides official bindings for several major state management libraries:
- Redux: via
inferno-redux - MobX: via
inferno-mobx - Cerebral: via
@cerebral/inferno
- Redux: via
Async data fetching with the loader attribute
masterinferno-routersupports async data fetching before navigation using theloaderattribute on aRoute. This allows you to fetch necessary data before the component is rendered. You can then access this data within the component using theuseLoaderDatahook.Example usage:
<Route path="/about" component={About} loader={() => fetch(new URL('/api/about', BACKEND_HOST))} />Use Fragments to render multiple elements without a container
masterFragments allow you to return an array of elements from a component's
rendermethod, creating an invisible layer that groups content without adding an extra DOM node.Syntax Options:
- Short syntax:
<> ... </>(requiresbabel-plugin-inferno) - Long syntax:
<Fragment> ... </Fragment>or<Inferno.Fragment> ... </Inferno.Fragment>(allows specifyingkeyfor dynamic lists) - API:
createFragment(children, childFlags, key) - createElement:
createElement(Inferno.Fragment, {key: 'test'}, ...children) - hyperscript:
h(Inferno.Fragment, {key: 'test'}, children)
import { Component, Fragment } from 'inferno'; class MyApplication extends Component { render() { return ( <> <span id="hi">Hi</span> <div id="okay">Okay</div> </> ); } } // Long syntax with keys for dynamic lists class ListApp extends Component { render() { const list = [ <Fragment key="coffee"> <dt>Coffee</dt> <dd>Black hot drink</dd> </Fragment/> ]; return <dl>{list}</dl>; } }- Short syntax:
Implement Controlled Components
masterIn Inferno, mutable state should typically be kept in the component's
stateproperty and updated viasetState().A controlled component is a form element (like
<input>,<textarea>, or<select>) where the Inferno component's state acts as the single source of truth for the element's value, preventing the DOM from holding independent state.Use new Component lifecycle methods
masterInferno v6 introduces lifecycle methods compatible with React. When using these new methods, the older lifecycle methods (
componentWillMount,componentWillReceiveProps,componentWillUpdate) will not be called.New methods:
getDerivedStateFromPropsgetSnapshotBeforeUpdate
Note: Inferno does not use
UNSAFE_prefixes; use the standard method names.How global animations work
masterGlobal animations allow you to animate a component between two different "pages" (where the elements do not share the same parent).
When one page is mounted immediately after another is unmounted,
inferno-animationperforms a FLIP-animation between the two positions. To enable this, assign the same string value to theglobalAnimationKeyattribute on both elements.Understand Inferno's event system
masterInferno uses a partially synthetic event system that delegates specific events for better performance.
Key differences from React:
- Inferno does not rename events by default. While React uses
onChange, Inferno uses the native DOM event name (e.g.,onInput). - Events should be camel cased. Lower case events will bypass Inferno's system and use the native browser event system.
Supported synthetic events:
onClick,onDblClickonFocusIn,onFocusOutonKeyDown,onKeyPress,onKeyUponMouseDown,onMouseMove,onMouseUponTouchEnd,onTouchMove,onTouchStart
- Inferno does not rename events by default. While React uses
Comparison with React-Router
masterIf you are coming from
react-router, note the following differences ininferno-router:- No React Native support: Unlike
react-router,inferno-routerdoes not provide official support for React Native. - Unified Package: There is no separate
inferno-router-dompackage; all web-related routing functionality is contained withininferno-router. - Feature Parity: It is a port of
react-routerv4/v5, with added support for theloaderattribute (from v6) for async data fetching. - Missing Features: Currently,
inferno-routerdoes not support download progress, form submission, or redirect support (in the sense of exposing response headers/status to the render method).
- No React Native support: Unlike
V6 VNode property changes
masterSeveral internal VNode properties have changed or been removed in v6:
domproperty: Not always populated for Components or Fragments. UsefindDOMNodefrominferno-extrasinstead.parentVNode: This property has been removed.- Text children: For optimization, single text children are no longer wrapped in another VNode. The
childrenproperty of a VNode containing only text will now hold the string directly (e.g.,vNode.children === 'Hello').