- Call super(props)
- Do: Set a initial state
- Don't: Cause side-effects (send a http request, store something in your local storage of the browser, send some analytics to Google Analytics or set timeouts)
Whenever your props change for your class-based component, you can sink your state to them (that will actually be very rare niche cases)
- Do: Sync state
- Don't: Cause side-effects
Use it only to prepare the data as you need it to lay out JSX code
- Prepare & structure your JSX code
Only once all child components were rendered and their lifecycle hooks finished your lifecycle here will finish
- Do: Cause side-effects (Make HTTP request to get new data from the web)
- Don't: Update State (this triggers re-render. Exception: it's inside the then block of a promise after you sent a HTTP request, but don't call set state in here synchronously)
Is also the best place to attach any event listeners you need to add for specific functionality.
componentDidMount() {
document.addEventListener("keydown", this.handleKeyPress)
}
componentWillUnmount() {
document.removeEventListener("keydown", this.handleKeyPress)
}
- Do: Sync state to props
- Don't: Cause side-effects
You would use it to initialize the state of a component that updates based on props you're getting (update props you're getting most likely). Rarely used because there is a more elegant way of updating your state or managing your components based on external properties.
example of use:
- form control which gets external properties and then you internally want to handle user input but initialize your state or update your state based on outside changes.
- Has to return
true
orfalse
- Used for performance improvements
- Do: Decide whether to continue or not
- Don't: Cause side-effects
Decide whether or not React should continue evaluating and re-rendering the component. (For performance optimization and should be used carefully: you can break your components).
This method is a useful way to optimize performance. For example, the default behavior is that your component re-renders when it receives new props, even if the props haven't changed. You can use shouldComponentUpdate() to prevent this by comparing the props. The method must return a boolean value that tells React whether or not to update the component. You can compare the current props (this.props) to the next props (nextProps) to determine if you need to update or not, and return true or false accordingly.
class OnlyEvens extends React.Component {
constructor(props) {
super(props);
}
// important code below this line
shouldComponentUpdate(nextProps, nextState) {
console.log('Should I update?');
// only updates if the number is even
function isEven(num) { return num % 2;}
return isEven(nextProps.value) == 0 ? true : false;
}
// important code above this line
componentWillReceiveProps(nextProps) {
console.log('Receiving new props...');
}
componentDidUpdate() {
console.log('Component re-rendered.');
}
render() {
return <h1>{this.props.value}</h1>
}
};
class Controller extends React.Component {
constructor(props) {
super(props);
this.state = {
value: 0
};
this.addValue = this.addValue.bind(this);
}
addValue() {
this.setState({
value: this.state.value + 1
});
}
render() {
return (
<div>
<button onClick={this.addValue}>Add</button>
<OnlyEvens value={this.state.value}/>
</div>
);
}
};
- Prepare & structure your JSX code
Basically the same as the render() in Creation Lifecycle.
And if they receive new props or state they will iterate their own lifecycle hooks
- Returns a snapshot value or
null
- This value will be used in componentDidUpdate() once the DOM has re-rendered
- Do: Last-minute DOM ops
- Don't: Cause side-effects
Returns a snapshot object which can freely configure.
example of use:
Last minute DOM operations - not changes - but scrolling position of the user. Imagine that your upcoming update of the component will re-render the DOM and will add new elements on the DOM and you want to restore the scrolling position of the user once the updating is done.
- Do: Cause side-effects (Make HTTP request to get new data from the web, but you'll have to watch out to not enter an infinite loop - a typical problem you'll be facing)
- Don't: Update State (this triggers re-render. Exception: it's inside the then block of a promise after you sent a HTTP request, but don't call set state in here synchronously)
Signals that you are now done with the updating, that the render method has been executed and now can cause side effects.
For internal changes
- Same as before but different context
- Same as before but different context
- used for clean up
Removing event listeners is an example of one such clean up action.
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
message: ''
};
this.handleEnter = this.handleEnter.bind(this);
this.handleKeyPress = this.handleKeyPress.bind(this);
}
// important code below this line
componentDidMount() {
document.addEventListener("keydown", this.handleKeyPress)
}
componentWillUnmount() {
document.removeEventListener("keydown", this.handleKeyPress)
}
// important code above this line
handleEnter() {
this.setState({
message: this.state.message + 'You pressed the enter key! '
});
}
handleKeyPress(event) {
if (event.keyCode === 13) {
this.handleEnter();
}
}
render() {
return (
<div>
<h1>{this.state.message}</h1>
</div>
);
}
};