React Lifecycle & Lifecycle methods Flashcards
What is the first phase of a React Lifecycle?
An instance of the component is created and two methods are called: GetDefaultProps and GetInitialState
What are two methods called on the initialization on creation of a component?
GetDefaultProps: is used to define any default props.
getInitialState: enables to set the initial state value, that is accessible inside the component via this.state.
What happens after the constructor define props and state?
componentWillMount is called and than the render method is executed.
What does the render method return on initialization?
It will return component markup which can be in the form of a single child or null/false.
After the first render method, what is called next?
ComponentDidMount
When is componentWillUnmount get called?
Before the component is removed from the DOM.
What is componentDidMount?
This is invoked immediately after mounting occurs, aka the first execution of render. Any changing of state in here will trigger another execution of render.
What is componentWillMount?
It is invoked immediately before mounting occurs. It is called before the first render(), thus setting state synchronously in here will not trigger a render.
props
this.props contains the props that were defined by the caller of the component.
state
contains data specific to this component that may change over time. The state is user defined.
What should you never do with state?
You should never mutate state, to set state call setState() to replace the state instead of mutating it.