react-class-binder
v1.0.1
Published
Automatically binds React method when using plain ES6 classes
Downloads
1
Readme
React Class Binder
Automatically bind methods defined in a React component using the ES2015 class syntax (similar to how React.createClass works).
This is a small package that uses a "trick" that allows mixin behavior in ES2015 using "subclass factories" so that you don't need to manually call the function in the constructor()
function, and you don't need to resort to still unstandardized syntax like decorators or ES2015 class properties. You can read more about it here.
Installation
To install the stable version:
npm install --save react-class-binder
Usage
This package uses a cool trick with ES2015 classes to make it extremely easy to use:
import React from 'react'
import binder from 'react-class-binder'
export default class ComponentName extends binder(React.Component) {
// ...component stuff here
}
No other configuration or options are needed! react-class-binder
will only bind what is absolutely necessary, so it won't touch React related method, nor will it rebind any method on any class that extends your class. So any libraries which use inheritance or higher-order-components will work fine with react-class-binder
(Including react-css-modules
and radium
)!
Alternate-Usage:
I also added (starting with version 1.1.0) a second way to use this if you don't want to import react-class-binder
in each method. It replaces the React
import with one that has BinderComponent
added to it. So you can use it like this:
import React from 'react-class-binder/react'
export default class ComponentName extends React.BinderComponent {
// ...component stuff here
}
Everything else is exactly the same. Please note that this method should be consitered beta as I haven't used it as extensively as I've used the mixin method.
Full Example
import React from 'react'
import binder from 'react-class-binder'
// V That's all that's needed!
export default class ReverseP extends binder(React.Component) {
static propTypes = {
text: React.PropTypes.string.isRequired
}
reverseText () {
// `this` is magically bound to the correct context!
return this.props.text.split('').reverse().join('')
}
// Since `this` is already "correctly bound" in React's functions, `react-class-binder` skips these
componentDidMount () {
console.log('Component Mounted!')
}
componentWillUnmount () {
console.log('Component About to Unmount!')
}
render () {
return <p>{this.reverseText()}</p>
}
}
FAQ
Why?
I was using the reverseText = () => {
trick for a while to allow my React functions to be "correctly bound", however it always rubbed me the wrong way. It was still unfinalized syntax, it was somewhat unintuitive to others that didn't know the trick, and it became annoying when I wanted to use async functions in a class once. So I made this package to avoid having to use that.
Why not just use one of the other packages for this?
One of them required decorators, which was a no-start because I'm trying to avoid non-standard syntax. A few others I saw used functions that were called in the constructor, which was a bit more boilerplate than I would have preferred to have in each component, and they didn't play well with other libraries that inherited from your class like react-css-modules
. This is simple, small, and works in all cases I've tried so far.
Does this impact performance?
react-class-binder
does all binding when the object is instantiated (in React, this is basically the componentWillMount lifecycle method). That is the only place that will take longer when using this package, and even that is so small that I can't reliably measure it.
Inspiration & Thanks
- Thanks to
react-autobind
andautobind-decorator
for the idea, and for helping me figure out the edge cases I'd need to solve. - Thanks to Dr. Axel Rauschmayer of ②ality.com for the "mixins" idea.
Contributing
The code is written in ES6 using Javascript Standard Style. Feel free to make PRs adding features you want, but please try to follow Standard. Also, documentation/readme PRs are more then welcome!
License
MIT Copyright (c) Gregory Benner