function
keyword, and commaclass
syntax. The constructor
method now assumes role of componentWillMount
class Photo extends React.Component {
render() {
return <img alt={this.props.caption} src={this.props.src} />;
}
}
// The ES5 way
var Photo = React.createClass({
handleDoubleTap: function(e) { … },
render: function() { … },
});
// ES6 way
class Photo extends React.Component {
handleDoubleTap(e) { … }
render() { … }
}
props
from their parent, but manage their own state.props
are public properties; state
are private properties.componentWillMmount
will fire in a parent component before it fires in a child component. Once rendered, componentDidMount
is fired on the lowest child component, and then up the chain.Various methods are executed at specific points in a component’s lifecycle:
componentWillMount
(in ES6+, this is now replaced by constructor
method)
componentDidMount
componentDidMount()
method of child components is invoked before that of parent components.componentWillReceiveProps
render()
is called by updating the state using this.setState()
.this.setState()
within this function will not trigger an additional render.shouldComponentUpdate
forceUpdate
is used.false
when you’re certain that the transition to the new props and state will not require a component update.shouldComponentUpdate
always returns true to prevent subtle bugs when state is mutated in place, but if you are careful to always treat state as immutable and to read only from props
and state
in render()
then you can override shouldComponentUpdate
with an implementation that compares the old props and state to their replacements.shouldComponentUpdate
to speed up your app.componentWillUpdate
componentDidUpdate
componentWillUnmount
componentDidMount
.var Hello = React.createClass({
render: function() {
return (
<div className="class">Hello World</div>
);
}
});
React.render(<Hello />, document.getElementById('app'));
var GreetUser = React.createClass({
getInitialState: function(){
return {
username: '@karloespiritu'
}
},
render: function() {
return (
<div>
Hello {this.state.username}
</div>
);
}
});
Use this built-in functions inside a component class
//triggered once before first render
componentWillMount: function(){
// Calling setState here does not cause a re-render
console.log('This will mount in this component');
},
// Triggered once after the first render
componentDidMount: function(){
// You have now access to this.getDOMNode()
alert('In Component Did Mount');
},
// Invoked whenever there is a prop change
// Called before render
componentWillReceiveProps: function(nextProps){
// Not called for the initial render
// Previous props can be accessed by this.props
// Calling setState here does not trigger an additional re-render
alert('In Component Will Receive Props');
},
// Called IMMEDIATELY before a component is unmounted
componentWillUnmount: function(){
render: function(){
return (
<div>
Hello, {this.state.name}
</div>
)
}
var MyList = React.createClass({
render: function() {
var item = function(listItem) {
return <li>{listItem}</li>;
};
return <ul>{this.props.items.map(item)}</ul>;
}
});