NuclearJS addons for React
Addons to quickly help you start up with React using NuclearJS, inspired by react-redux.
Provides NuclearJS reactor
context via the <Provider reactor={reactor} />
component and binds to getters via connect
higher order component (or decorator).
Install
npm install --save nuclear-js-react-addons
// ES6
// ES5var NuclearAddons = var Provider = NuclearAddonsProvider;var connect = NuclearAddonsconnect;var nuclearMixin = NuclearAddonsnuclearMixin;
Documentation
Provider
Container component allowing a reactor
to be exposed via context.
Simple App
// in a App.js fileComponent { <Provider reactor=reactor> <SomeComponent /> </Provider> }
Now the reactor is provided as the reactor key of the react context if you declare
contextTypes: reactor: ReactpropTypesobjectisRequired
which you won't have to do manually, because both a mixin and a component are available for you to use.
connect
For usage with ES6 class syntax this Higher Order Component can be used as a decorator or as a javascript function.
Example using the decorator pattern:
;; @
Or as a function
;; { // get the reactor and your dataBindings // from the props passed in from the wrapper const reactor foo bar } = thisprops; return <div> foo bar </div> } { return foo: gettersfoo bar: gettersbar } const ConnectedChild = Child
nuclearMixin
// Component must be a descendent where `context.reactor` existsvar Child = React;
provideReactor
Deprecated in 0.3.0, use <Provider reactor={reactor}>
instead
Helper to help you provide your reactor to a react component tree using react contexts.
Simple App
// in a App.js filevar App = React;
elsewhere
var Nuclear = ;var reactor = ;var provideReactor = provideReactor;// orvar provideReactor = ;var App = ;// Wrap your App into a Higher order Component => HoCvar App = ; // If you don't pass the reactor as a prop you will have a warningReact;
or decorator pattern (es7)
@provideReactorComponent { return <Child/> }
nuclearComponent
Deprecated in 0.3.0, use connect()
instead
If you prefer to stay away from mixin, there's also a nuclear component to suit your needs. It also support the decorator pattern
Example using the decorator pattern:
;; @Component { // get the reactor and your dataBindings // from the props passed in from the wrapper const reactor foo bar } = thisprops; return <div> foo bar </div> }
or simply still using es5
var nuclearModule = ;var nuclearComponent = nuclearComponent; var Child = React; nuclearModuleexports = ;
Examples
Resubscribe to getters when props update
This is possible by extending a @connect
component.
@ { superprops context } { <span>thispropsvalue</span> }
this.props.value
will always be bound to the initial value of this.props.key
, if we want a component that updates when props change we can simply extend it.
{ superprops context } { // any logic to check next props against current props can go here if thispropskey !== nextPropskey this; }
Our ResubscribingNuclearComponent
now rebinds all getters to the new props.
Additional examples here
Tests
Run tests with karma via npm test
Inspirations
Inspired/adapted from
Thanks to those for the help.