1.0.1 • Published 9 years ago
react-action-mixin v1.0.1
react-action-mixin
This mixin is gives a better, nicer, prettier way of providing handlers to the React.js components that need to act on a certain event. Usally those are the "smart" components (or "controlled", which ever you prefer).
Installation
npm instal --save react-action-mixinReact.createClass
With React's standard "classes" just use the mixin property, like so:
const ActionMixin = require('react-action-mixin');
const MySmartComponent = React.createClass({
mixin: [ActionMixin],
// ...
});And you're all set and good to go
React.Component ES6 class
Since React, when using ES6 classes, doesn't support any type of mixins we cannot use it so freely. There is a way around it, though, like so:
// `smart_component.jsx`
import React, { Component } from 'react';
import ActionMixin from 'react-action-mixin';
class SmartComponent extends Component {
constructor(props) {
super(props):
Object.assign(this, ActionMixin);
}
}
export default SmartComponent;
// `my_smart_component.jsx`
import React from 'react';
import SmartComponent from './smart_component';
class MySmartComponent extends SmartCompoent {
// ...
}This way you can use it in your project when you prefered the ES6 version.
API
onAction(string, ...additional_parameters)
stringwill be used to call the handler that you want on the component. The function invoked will use the following startegy to build the function name:'onAction'+camelCasedandcapitalizedversion of thestringprovided, for example:this.onAction('do stuff')will callthis.onActionDoStuff()additional_parameters- any number of parameters provided here will be accessible as after the last argument from the called handler, for example:
<input onChange={this.onAction('do stuff', { foo: 'bar' }) value={email} />
onActionDoStuff(ev, fooBar) {
// foobar => { foo: 'bar' }
}Keep in mind if there would be more arguments from invoked handler
fooBarobject may be accessible as the 3rd, 4th or n-th argument
onThrottledAction(string, options, ...additional_parameters)
This function will work the same as onAction, with the following exceptions:
- function name that will be build will use the
stringin the same manner as inonAction, but the first part is NOTonAction, butonThrottledAction optionsobject is used to pass down tothrottlefunction fromlodash, it will not be passed when the handler is funally invoked. To know more about theoptionsobject andthrottlefunction, please follow this link.optionsby default is set to
options = { wait: 0 };Examples
const ActionMixin = require('react-action-mixin');
const MySmartComponent = React.createClass({
mixin: [ActionMixin],
getInitialState() {
return {
email: '',
password: ''
}
},
render() {
const { email, password } = this.state;
return (
<form onSubmit={this.onAction('submit')>
<input onChange={this.onAction('change') name="email" value={email} />
<input onChange={this.onAction('change') name="password" value={password} />
<button type="submit">Log In</button>
</form>
);
},
onActionSubmit(ev) {
ev.preventDefault();
// do login stuff
},
onActionChange(ev) {
const { name, value } = ev.target;
this.setState({ [name]: value });
}
});