xferno
React hooks for infernojs.
Status: experimental. See the "How it works" note at the bottom of this readme.
Quick example
; { const count setCount = ; return <button onClick= >count</button> ;}
Installation
npm install --save xferno
Configuration
Be sure to modify your babel-plugin-inferno
to have imports: 'xferno'
:
Usage
The following "primitive" hooks are built into xferno. Custom hooks can be composed from these.
- useState
- useEffect
- useMemo
- useDisposable
- useSelector
- useDispatch
These each work similarly to the React equivalents. (Some of these have no React equivalents, though, so... keep reading.)
useState
; { // state can be a primitive for an object, etc. setState can be called // with a callback setState((s) => s) or with the new value for state // setState({ password: 'hoi' }) const state setState = ; return <input type="password" value=statepassword onInput= /> ;}
useEffect
useEffect
can be called with no arguments, in which case it will be invoked only once
for the entire life of the component. If it is given a second argument, the effect function
will be invoked any time the second argument changes. If the effect function returns a
function, that function will be invoked when the component is disposed or before the
effect re-runs, which ever comes first.
; { const time setTime = ; ; return <h1>time</h1> ;}
useMemo
useMemo
is used to memoize an expensive operation. If no second argument is passed,
it will only run once (when the component first initializes) otherwise, it will re-evaluate
any time the second argument changes.
; { const name = ; return <h1>name</h1> ;}
useDisposable
useDisposable
is like a combination of useEffect and useMemo. It allows the caller to return
a value which can be consumed by the component, but also which is cleaned up any time the first
argument is re-invoked or whenever the component is destroyed.
The first argument is a function which must return an object with a value property and a dispose function.
; { const url = ; return <video src=url></video> ;}
useSelector
useSelector
provides a convenient mechanism for extracting a subset of Redux state for your component.
It is used in conjunction with Redux or a similarly shaped state store.
It is expected that context.store
is a Redux (or similar) store, with dispatch
, subscribe
, and getState
methods.
You can create your own component which provides this context, or you can use ReduxStoreProvider
to provide it (as detailed further down).
; { // Assuming we have Redux state that looks something like { name: 'World' } const name = ; return <h1>Hello name</h1> ;}
useDispatch
useDispatch
provides the Redux dispatch function to your component.
This has the same requirements regarding Redux / store as useSelector
.
; { // Assuming we have Redux state that looks something like { count: 0 } const count = ; const dispatch = ; return <button onClick= > count </button> ;}
ReduxStoreProvider
If you want to use Redux (or something similar), you need to provide the Redux store to the useSelector and useDispatch hooks.
To do this, you can use ReduxStoreProvider
somewhere near the root of your application.
import ReduxStoreProvider from 'xferno'; // ... reducer, initial state, etc omitted for brevity... const store = ; { return <ReduxStoreProvider => <OtherComponentsHere /> </ReduxStoreProvider> ;}
How it works
This overrides inferno's createComponentVNode
function and wraps all functional components in a hook-aware wrapper. The overriding of a core inferno function is what makes this an experimental library. This also modifies a global hook context in order to track hook state correctly across components. The level of hackery here means that more time and production-grade vetting is required before the experimental classification is removed.
Things that could use improving / adding:
- TypeScript definitions
- Better minification
- More intelligent hook state tracking
- Right now, we wrap all functional components, but it would be far prefrable to somehow detect only those components that actually use hooks, and only wrap those.
License
MIT