Oliver Zeigermann / @DJCordhose
Online version at: http://djcordhose.github.io/serious-javascript/slides/react-intro.html
http://facebook.github.io/react/blog/2014/09/12/community-round-up-22.html
<!DOCTYPE html>
<html>
<head>
<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="example"></div>
<script type="text/jsx">
/** @jsx React.DOM */
React.renderComponent(
<h1>Hello, world!</h1>,
document.getElementById('example')
);
</script>
</body>
</html>
Run
/** @jsx React.DOM */
var mountNode = document.getElementById('example');
var HelloMessage = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
React.renderComponent(<HelloMessage name="Olli" />, mountNode);
React.createClass
: creates a component class
render
: creates a virtual DOM tree (maybe using JSX)
this.props
/ input: something passed into component instance as input
var mountNode = document.getElementById('example');
var HelloMessage = React.createClass({displayName: 'HelloMessage',
render: function() {
return React.DOM.div(null, "Hello ", this.props.name);
}
});
React.renderComponent(HelloMessage( {name:"Olli"} ), mountNode);
React.renderComponent(ReactComponent component, DOMElement container)
: renders a component into a DOM element
React.renderComponentToString
: for server side rendering
var TodoList = React.createClass({
render: function() {
var createItem = function(itemText) {
return <li>{itemText}</li>;
};
return <ul>{this.props.items.map(createItem)}</ul>;
}
});
<TodoList items={['Stuff', 'More Stuff']} />
var TodoApp = React.createClass({
getInitialState: function() {
return {items: ['Do stuff', 'Do more stuff']};
},
render: function() {
return (
<div>
<h3>TODO</h3>
<TodoList items={this.state.items} />
</div>
);
}
});
React.renderComponent(<TodoApp />, mountNode);
var TodoApp = React.createClass({
render: function() {
return (
<div>
<h3>TODO</h3>
<TodoList items={this.state.items} />
<form onSubmit={this.handleSubmit}>
<input onChange={this.onChange} value={this.state.text} />
<button>{'Add #' + (this.state.items.length + 1)}</button>
</form>
</div>
);
}
onChange: function(e) {
this.setState({text: e.target.value});
},
handleSubmit: function(e) {
e.preventDefault();
this.state.items.push(this.state.text);
this.setState({items: this.state.items, text: ''});
}
});
var HelloMessage = React.createClass({
getInitialState: function() {
return { model: this.props.greeting};
},
updateModel: function(event) {
this.setState({model: event.target.value});
},
reset: function() {
this.setState({model: ""});
},
render: function() {
return (<div>
<input onChange={this.updateModel} value={this.state.model} />
<p>{this.state.model}, World</p>
<input type="button" value="Clear" onClick={this.reset} />
</div>);
}
});
var mountNode = document.getElementById('example');
React.renderComponent(<HelloMessage greeting="Hello" />, mountNode);
Run
<input id="in" onkeyup="setModel(this.value)">
<p><span id="log"></span>, World</p>
<input type="button" value="Clear" onclick="reset()">
<script>
var model;
function setModel(value) {
model = value;
document.getElementById("log").innerHTML = model;
document.getElementById("in").value = model;
}
function reset() {
setModel("");
}
window.onload = function() {
setModel("Hello");
};
</script>
Run
<input id="in">
<p><span id="log"></span>, World</p>
<input id="btn" type="button" value="Clear">
<script>
var model;
function setModel(value) {
model = value;
$("#log").html(model);
$("#in").val(model);
}
$(document).ready(function () {
$("#in").on("keyup", function (event) {
setModel($("#in").val());
});
$("#btn").on("click", function () {
setModel("");
});
setModel("Hello");
});
</script>
Run
<body np-app ng-controller="HelloController as helloController">
<input ng-model="helloController.greeting.text">
<p>{{helloController.greeting.text}}, World!</p>
<input type="button" value="Clear" ng-click="helloController.clear()">
</body>
function HelloController() {
this.greeting = {
text: 'Hello'
};
}
HelloController.prototype.clear = function() {
this.greeting.text = '';
};
Run