
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
react-binding
Advanced tools
React-binding is lightweight utility for two-way data binding in React.
import React from 'react';
import ReactDOM from 'react-dom';
import Binder from 'react-binding';
export default class Form extends React.Component {
constructor(props){
super(props);
this.state = {data: {}};
},
render {
return (
<div>
<input valueLink={Binder.bindToState(this,"data", "Employee.FirstName")} />
<div>FirstName: {this.state.data.Employee.FirstName}</div>
</div>
)}
});
ReactDOM.render(
<Form />,
document.getElementById('content')
);
react-binding offers two-way data binding support for:
Each bindTo return and uses interface called "value/onChange". Each bindTo component is passed a value (to render it to UI) as well as setter to a value that triggers a re-render (typically at the top location).
The re-render is done in the component where you bind to the state via (bindToState, bindArrayToState).
BindTo can be nested - composed to support components composition. Then path is concatenate according to parent-child relationship.
npm install react-binding
bower install react-binding
npm install -g browserify
npm install reactify
browserify ./index.js > bundle.js
minimal example
import React from 'react';
import ReactDOM from 'react-dom';
import Binder from 'react-binding';
export default class Form extends React.Component {
constructor(props){
super(props);
this.state = {data: {}};
},
render {
return (
<div>
<input valueLink={Binder.bindToState(this,"data", "Employee.FirstName")} />
<div>FirstName: {this.state.data.Employee.FirstName}</div>
</div>
)}
});
ReactDOM.render(
<Form />,
document.getElementById('content')
);
Note: React-binding as mixins - use npm install react-binding@0.6.4
It enables to bind to object property with path expression
<input type='text' valueLink={Binder.bindToState(this,"data","Employee.Contact.Email")} />
<TextBoxInput model={Binder.bindToState(this,"data","Employee.Contact.Email")} />
var TextBoxInput = React.createClass({
render: function() {
var valueModel = this.props.model;
var handleChange = function(e){
valueModel.value = e.target.value;
}
return (
<input type='text' onChange={handleChange} value={valueModel.value} />
)
}
});
It enables to bind to complex object with nested properties and reuse bindings in components.
<PersonComponent personModel={Binder.bindToState(this,"data","Employee")} />
<PersonComponent personModel={Binder.bindToState(this,"data","Deputy")} />
<input type='text' valueLink={Binder.bindTo(this.props.personModel,"Contact.Email")} />
var PersonComponent = React.createClass({
render: function() {
return (
<div>
<input type='text' valueLink={Binder.bindTo(this.props.personModel,"FirstName")} />
<input type='text' valueLink={Binder.bindTo(this.props.personModel,"LastName")} />
<input type='text' valueLink={Binder.bindTo(this.props.personModel,"Contact.Email")} />
</div>
);
}
});
It enables binding to collection-based structures (array). It enables to add and remove items.
<HobbyList model={Binder.bindArrayToState(this,"data","Hobbies")} />
var HobbyList = React.createClass({
render: function() {
if (this.props.model.items === undefined) return <span>There are no items.</span>;
var hobbies = this.props.model.items.map(function(hobby, index) {
return (
<Hobby model={hobby} key={index} onDelete={this.handleDelete} />
);
},this);
return (
<div>{hobbies}</div>
);
}
});
handleAdd: function(){
return this.props.model.add();
},
handleDelete: function(hobby){
return this.props.model.remove(hobby);
},
It enables binding to collection-based structures (array) for nested arrays. It enables to add and remove items.
<HobbyList model={Binder.bindArrayTo(this,parent,"Hobbies")} />
var HobbyList = React.createClass({
render: function() {
if (this.props.model.items === undefined) return <span>There are no items.</span>;
var hobbies = this.props.model.items.map(function(hobby, index) {
return (
<Hobby model={hobby} key={index} onDelete={this.handleDelete} />
);
},this);
return (
<div>{hobbies}</div>
);
}
});
handleAdd: function(){
return this.props.model.add();
},
handleDelete: function(hobby){
return this.props.model.remove(hobby);
},
Value converters
Example - date converter -> using parameters 'dateFormat' is optional
var dateConverter = function() {
this.parse = function (input, dateFormat) {
if (!!!input) return undefined;
if (input.length < 8) return undefined;
var date = moment(input, dateFormat);
if (date.isValid()) return date.toDate();
return undefined;
}
this.format = function (input,dateFormat) {
if (!!!input) return undefined;
return moment(input).format(dateFormat);
}
}
using converter
<DatePicker label="From" model={Binder.bindToState(this,"data", "Duration.From", converter, 'DD.MM.YYYY')} error={this.validationResult().Duration.From} />
<DatePicker label="To" model={Binder.bindToState(this,"data", "Duration.To", converter, 'DD.MM.YYYY')} error={this.validationResult().Duration.To} />
JSON models trees, and most application domains are graphs. Binding supports concept for references to allow JSON to be used to represent graph information.
{
todosById: {
"44": {
name: "get milk from corner store",
done: false,
prerequisites: [{ $type: "ref", value: ["todosById", 54] }]
},
"54": {
name: "withdraw money from ATM",
done: false,
prerequisites: []
}
},
todos: [
{ $type: "ref", value: ["todosById", 44] },
{ $type: "ref", value: ["todosById", 54] }
]
};
hobby form - data binding only
hobby form with validation using business-rules-engine
value converters
For more information on react-binding please check out my blog.
FAQs
Binder - react two-way data binding for complex objects and arrays.
The npm package react-binding receives a total of 146 weekly downloads. As such, react-binding popularity was classified as not popular.
We found that react-binding demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.