State, Lifecycle and Event Handlers
Updated: 03 September 2023
React Components
Class Components
React components can also be written as ES6 classes instead of functions. This can be done by extending the React.Component
class
As expected, we can also add props to these components as with functional components as follows
State
The constructor
is called before a React component is mounted and is used to set up the initial component state. It is important to call the super(props)
function otherwise the constructor may not work correctly
Initial state can be defined as well as updated using the constructor
and setState
functions respectively
Previous State
The setState
will update the component when React reaches it in the update queue in order to be more efficient. The method updates the state asynchronously and has a componentDidMount
method that is called when that happens, thereby allowing us to update a component based on previous state
We can use the setState
function with a function that takes two inputs prevState, props
in order to update the properties based on that function
Future State
Since the state updates asynchronously, we cannot immediately use the new state after calling the setState
function
State is Immutable
State is immutable and hence should not be manipulated directly. For example, we cannot do the following
Lifecycle Methods
Each class component goes through a lifecycle which contains multiple phases and methods that can be defined
Mounting
constructor(props)
is called when a component is initialized. This is only called oncecomponentWillMount()
is called just before a component mountsrender()
is called when a component is renderedcomponentDidMount()
is called when a component has been mounted - we will typically make network requests in this phase
Updating
These methods happen when a component’s state changes
componentWillReceiveProps(nextProps)
os called when a component has updated and is receiving new propsshouldComponentUpdate(nextProps, nextState)
will decide whether a component should run thecomponentWillUpdate
,render()
, andcomponentDidUpdate
functions and must return aboolean
componentWillUpdate(nextProps, nextState)
is called when a component is about to be updatedrender()
componentDidUpdate(prevProps, prevState)
is called after a component has updated
Unmounting
The componentWillUNmount()
function is called just before a component is removed from the DOM and is used for any cleanup such as cancelling timers and network requests
Event Handlers
Events are handled similar to the way they are handled in HTML, aside from the fact that they are defined in camelCase and use the {}
instead of ""
when attaching them to an element
Event handlers are defined withing a Class component, this can be done as follows
If we need access to the correct this
for an event handler we need to bind
the function, this can be done in two ways
From the constructor
with as above
Or with the ES6
arrow function to pass forward the context
Passing State to Parents
At times it may be necessary to pass state from a child to a parent in order to change some other state elsewhere (either in the parent or in siblings by way of the parent), this can be done by passing the event handler down to the children components through their props, such as can be seen in the Button
class below which attaches the clickHandler
function defined in the App
class
The app below simply displays buttons and text below, and when a button is clicked the state of siblings as well as the button itself should be updated
Demo App
We can make a Demo App that makes use of all the above, the code can be found on This CodePen