Eirik Langholm Vullum PRO
JavaScript fanatic that loves node.js and React.
A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES
Application
DOM
mutate state
mutate state
read state
read state
:(
React
(virtual DOM)
(JavaScript)
DOM
(browser)
Application
state
patch DOM
patch DOM
props
change
const HelloWorldComponent = React.createClass({
render() {
return (
<div className="some-style">
<p>Hello World</p>
</div>
);
}
});
const HelloWorldComponent = React.createClass({
render() {
return (
React.createElement("div", { className: "some-style" },
React.createElement("p", null, "Hello World");
);
);
}
});
ReactDOM.render(
<HelloWorldComponent/>,
document.querySelector('#mountNode');
);
But what about state, changes and everything that makes my app dynamic?
const HelloWorldComponent = React.createClass({
render() {
return (
<div className="some-style">
<p>{this.props.greeting}</p>
</div>
);
}
});
<HelloWorldComponent greeting='Hello World' />
<HelloWorldComponent greeting={'Hello ' + 'World'} />
<HelloWorldComponent greeting={getGreeting()} />
const HelloWorldComponent = React.createClass({
getInitialState() {
return {
count: 0
};
},
render() {
return (
<div className="some-style">
<p>{this.props.greeting}</p>
<p>{this.state.count}</p>
</div>
);
}
});
const HelloWorldComponent = React.createClass({
getInitialState() {
return {
count: 0
};
},
handleClick() {
this.setState({
count: this.state.count + 1
});
},
render: ...
});
const HelloWorldComponent = React.createClass({
getInitialState: ...
handleClick: ...
render() {
return (
<div onClick={this.handleClick} className="some-style">
<p>{this.props.greeting}</p>
<p>{this.state.count}</p>
</div>
);
}
});
var HelloWorldComponent = React.createClass({
getInitialState: ...
handleClick: function (event) {
event.preventDefault();
this.setState({
count: this.state.count + 1
});
},
render: ...
});
First render call
Subsequent
render calls
Updating state
getDefaultProps
getInitialState
componentWillMount
render
componentDidMount
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
Being removed from the DOM
componentWillUnmount
var MyComponent = React.createClass({
// lifecycle methods
getDefaultProps: ...
getInitialState: ...
componentWillMount: ...
componentDidMount: ...
componentDidUpdate: ...
componentWillReceiveProps: ...
componentWillUnmount: ...
shouldComponentUpdate: ...
// custom methods
handleThis: ...
handleThat: ...
computeThis: ...
computeThat: ...
// rendering
render: ...
});
<ChatApp />
<FriendList />
<MessageList />
<Friend />
<Friend />
<Friend />
<Friend />
<Message />
<Message />
<Message />
<MessageInput />
var ChatApp = React.createClass({
...
render() {
return (
<div>
<FriendList friends={this.state.friends} />
<MessageList messages={this.state.messages} />
<MessageInput handleInput={this.handleInput} />
</div>
);
}
});
var MessageList = React.createClass({
render: function() {
var messages = this.props.messages.map(function(message) {
return (
<Message
content: message.content,
from: message.from
/>
);
});
return <ul>{messages}</ul>;
}
});
var Message = React.createClass({
render: function() {
return (
<li>
<span>From: {this.props.from}</span>
<span>Content: {this.props.content}</span>
</li>
);
}
});
<ChatApp />
<FriendList />
<MessageList />
<Friend />
<Friend />
<Friend />
<Friend />
<Message />
<Message />
<Message />
<MessageInput />
this.state.friends
this.state.messages
this.handleInput
this.props.friends
this.props.messages
this.props.handleInput
<Surface>
<Layer>
<Group>
<Text>
<Image>
<ListView>
<div>
<span>
<p>
<ul>
<li>
<table>
vs.
<Image>
<ListView>
<MapView>
<Navigator>
<ScrollView>
<Text>
<TextInput>
<View>
<WebView>
...
<div>
<span>
<p>
<ul>
<li>
<table>
vs.
Pretty much anything!
By Eirik Langholm Vullum
Introtalk to React