react-class
Advanced tools
Comparing version 3.0.1 to 3.1.0
{ | ||
"name": "react-class", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "A carefully crafted base class for all your React components ", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -37,14 +37,36 @@ # react-class | ||
## auto-binding | ||
## Just auto-binding | ||
In order to get autobinding, just extend the class exported by `react-class` | ||
If you don't want to extend the class exported by `react-class` and instead just want autobinding, you can just import the `autoBind` function directly. | ||
```jsx | ||
import 'react-class/autoBind' | ||
var ReactClass = require('react-class'); | ||
// or | ||
class App extends ReactClass { ... } | ||
import { autoBind } from 'react-class' | ||
// or | ||
var autoBind = require('react-class/autoBind') | ||
```` | ||
After importing/require-ing it, call `autoBind` in the component constructor: | ||
```jsx | ||
import autoBind from 'react-class/autoBind' | ||
class MyApp extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
autoBind(this) | ||
} | ||
render() { | ||
// ... your rendering logic | ||
} | ||
} | ||
``` | ||
## FAQ | ||
@@ -51,0 +73,0 @@ |
"use strict"; | ||
var React = require('react') | ||
var autoBind = require('../autoBind') | ||
var skipMethods = { | ||
'constructor': 1, | ||
'render': 1, | ||
'shouldComponentUpdate': 1, | ||
'componentWillMount': 1, | ||
'componentDidMount': 1, | ||
'componentWillReceiveProps': 1, | ||
'componentWillUpdate': 1, | ||
'componentDidUpdate': 1, | ||
'componentWillUnmount': 1 | ||
} | ||
function autoBind(object){ | ||
var proto = object.constructor.prototype | ||
var names = Object.getOwnPropertyNames(proto).filter(function(key) { | ||
return skipMethods[key] !== 1 && typeof proto[key] === 'function' | ||
}) | ||
names.push('setState') | ||
names.forEach(function(key){ | ||
object[key] = object[key].bind(object) | ||
}) | ||
return object | ||
} | ||
class ReactClass extends React.Component { | ||
@@ -33,0 +7,0 @@ constructor(props){ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16944
13
360
87