Dynamic Rendering of Components in React
In this article, we will explore the concept of state and dynamic rendering of components in React. We will discuss how to create a component using a class, add local state to a class, and implement event methods (Lifecycle Methods). We will also cover the correct use of state data, single-way data binding, and event handling.
Replacing Function with Class to Create a Component
To create a component using a class, we need to follow five steps:
- Create a class that extends
React.Component. - Add a constructor to the class.
- Add local state to the class.
- Add event methods (Lifecycle Methods) to the class.
- Implement the
rendermethod to return the JSX element.
Adding Local State to a Class
To add local state to a class, we need to follow these steps:
- Create a constructor to initialize the state.
- Add a
rendermethod to return the JSX element. - Use the
this.stateproperty to access the state.
Implementing Event Methods (Lifecycle Methods)
To implement event methods (Lifecycle Methods), we need to follow these steps:
- Create a constructor to initialize the state.
- Add a
componentDidMountmethod to handle the mounting of the component. - Add a
componentWillUnmountmethod to handle the unmounting of the component. - Implement the
rendermethod to return the JSX element.
Correct Use of State Data
To use state data correctly, we need to follow these best practices:
- Use
setStateto update the state. - Do not modify the state directly.
- Use the
setStatemethod with a function as the first argument to update the state. - Use the
setStatemethod with a function as the second argument to update the state.
Single-Way Data Binding
To implement single-way data binding, we need to follow these steps:
- Use the
propsproperty to access the props. - Use the
this.stateproperty to access the state. - Use the
setStatemethod to update the state.
Event Handling
To handle events in React, we need to follow these steps:
- Use the
onproperty to attach an event handler. - Use a function as the event handler.
- Use the
preventDefaultmethod to prevent the default behavior of the event.
Example Code
Here is an example code that demonstrates the concepts discussed above:
import React, { Component } from 'react';
class Clock extends Component {
constructor(props) {
super(props);
this.state = {
date: new Date()
};
}
componentDidMount() {
this.timerID = setInterval(() => {
this.tick();
}, 1000);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}</h2>
</div>
);
}
}
ReactDOM.render(<Clock />, document.getElementById('root'));
This code creates a Clock component that displays the current time. The componentDidMount method creates a timer that updates the time every second. The componentWillUnmount method clears the timer when the component is unmounted. The tick method updates the state with the current time. The render method returns the JSX element that displays the time.
I hope this article has helped you understand the concepts of state and dynamic rendering of components in React.