React Class Helper
Helper for ES6 class with React (autobind, mixins, ...)
Installation
npm install react-class-helper
To see how to use ES6 class with React, please check this post
Usage
This library provides two elements: Component
and Mixins
. They can be used together or separately.
Component
Component
extends React's Component built-in class to provide auto-binding.
React.createClass has a built-in magic feature that bound all methods to this automatically for you. This can be a little confusing for JavaScript developers that are not used to this feature in other classes, or it can be confusing when they move from React to other classes.
Therefore we decided not to have this built-in into React's class model. You can still explicitly prebind methods in your constructor if you want.
So it means that currently you have to do this:
; Component { superprops; // Explicitly prebind methods in your constructor thisonClick = thisonClick; } { // If not prebind, this === window (global object in browser) this; } { return <button onClick=thisonClick>My button</button> ; }
With your component extending `Component` class, you can do this:
// Note, I left React module because the JSX tags are transformed// to `React.createElement` so we still need to import this module;; // Extending `Component` instead of `React.Component` { superprops; // Use `super(props, false);` to not autobind // Or `this.bind(['onClick']);` to bind only some methods } { // Automatically bind to class instance this; } { return <button onClick=thisonClick>My button</button> ; }
Mixins
Mixins
provides compatibility with React.createClass
mixins. Original idea from react-mixin.
Unfortunately, we will not launch any mixin support for ES6 classes in React. That would defeat the purpose of only using idiomatic JavaScript concepts.
There is no standard and universal way to define mixins in JavaScript. In fact, several features to support mixins were dropped from ES6 today. There are a lot of libraries with different semantics. We think that there should be one way of defining mixins that you can use for any JavaScript class. React just making another doesn't help that effort.
But if you still want to use mixins with ES6 class. See below how.
`Mixins(componentClass, mixins = [], options = {})`
componentClass
Component factory (not class instance).mixins
Array of mixin objects.options
defaultRule
Default rule to apply to property not defined inrules
rules
Map mixin properties to rules
// This is the default options rules: // Lifecycle methods componentWillMount: MixinsMANY componentDidMount: MixinsMANY componentWillReceiveProps: MixinsMANY shouldComponentUpdate: MixinsONCE componentWillUpdate: MixinsMANY componentDidUpdate: MixinsMANY componentWillUnmount: MixinsMANY // Compatibility hack getDefaultProps: MixinsMANY_MERGED getInitialState: MixinsMANY_MERGED defaultRule: MixinsONCE
Built-in rules - `Mixins.ONCE` Property can be defined only once in component or mixin - `Mixins.MUlTI` Property can be defined multiple times in component or mixin, execution order is from left to right in mixins array and then component. - `Mixins.MULTI_MERGED` Property can be defined multiple times in component or mixin, execution order is from left to right in mixins array and then component. Merge all results into one. Must returns objects or null.
Example:
;; // Define component { superprops; // `Component` class set `this.state` from `_getInitialState()` automatically // If you use the built-in `React.Component` you have to call it explicitly // // this.state = Object.assign({}, this._getInitialState()); } // If you use the built-in `React.Component` you must declare // this method explicitly // // _getInitialState() { // return {}; // } { this; } { console; } { return <button onClick=thisonClick>My button</button> ; } // Define some mixinsvar myMixin1 = { console; }; var myMixin2 = { console; } // Objects are ignored except 'statics' and 'propTypes' someObject: {} // Ignore propTypes: // Merge into `MyButton.propTypes` myProp: ReactPropTypesstring statics: // Merge into `MyButton` queries: {} { // Merge into `MyButton.defaultProps` return myProp: 'myProp' ; } { // Rename to `_getInitialState` to avoid React's warning // Call in component constructor and merge result return myState: 'myState'; } { // Throw error if defined in other mixin return true; }; // Set mixins to component;
Support
If you have any problem or suggestion please open an issue here.
License MIT
The MIT License
Copyright 2015, Simon Degraeve
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.