Last active
August 29, 2015 14:02
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @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')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is more cleaner than the React version of http://n12v.com/2-way-data-binding/