Skip to content

Instantly share code, notes, and snippets.

@insin
Last active August 29, 2015 14:02
Show Gist options
  • Save insin/3f4c479fc55eb2d0d4f1 to your computer and use it in GitHub Desktop.
Save insin/3f4c479fc55eb2d0d4f1 to your computer and use it in GitHub Desktop.
React version of http://n12v.com/2-way-data-binding/ without double conversion issue. Live version: http://bl.ocks.org/insin/raw/3f4c479fc55eb2d0d4f1/
/** @jsx React.DOM */
function c2f(c) {
return 9/5 * c + 32
}
function f2c(f) {
return 5/9 * (f - 32)
}
var TemperatureConverter = React.createClass({
getInitialState: function() {
return {c: 0, f: c2f(0)}
},
render: function() {
return <div>
<input type="number" value={this.state.c} onChange={this.onCelsiusChange}/>°C
<span> ⇄ </span>
<input type="number" value={this.state.f} onChange={this.onFahrenheitChange}/>°F
</div>
},
onCelsiusChange: function(e) {
var c = e.target.value
this.setState({c: c, f: c2f(c)})
},
onFahrenheitChange: function(e) {
var f = e.target.value
this.setState({c: f2c(f), f: f})
}
})
React.renderComponent(<TemperatureConverter/>, document.getElementById('app'))
<!DOCTYPE html>
<html>
<head>
<title>Temp</title>
<script src="http://fb.me/react-0.10.0.js"></script>
<script src="http://fb.me/JSXTransformer-0.10.0.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/jsx" src="app.jsx"></script>
</body>
</html>
@abhiomkar
Copy link

this is more cleaner than the React version of http://n12v.com/2-way-data-binding/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment