In AngularJS I can do:
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="firstname">
<h1>{{firstname}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.lastname = "Doe";
});
</script>
</body>
</html>
What is the equivalent way to do this in React? Here is what I have tried:
var app = document.getElementById("app");
var Main = React.createClass({
getInitialState: function(){
return {text: ""};
},
change: function(e){
var newValue = e.target.value;
this.setState({text: newValue});
},
render: function(){
return (
<div>
<input onChange={this.change} type="text"/>
<h1>{this.state.text}</h1>
</div>
);
}
});
React.render(<Main />, app);
Is it the best way for implementing this?
This way seems to be a little slow. Can you suggest another way?