How to get data of clicked component in ReactJS
Getting Started
ReactJS, an open-source JavaScript library, is a popular choice for building user interfaces, particularly for single-page applications. Here, we are going to explore a common scenario in ReactJS – getting the data of a clicked component.
Understanding Components
Think of components in ReactJS as the building blocks of your application. Each component corresponds to an element on your web page. Let's say you are building a simple app to list your favorite books. Each book on the list can be a component.
Event Handling in ReactJS
When a user interacts with your application, say, by clicking on a book, an event is triggered. Just like in real life, an event, say a party, doesn't happen in isolation. It has related data like the venue, the time, and the guest list. Similarly, in ReactJS, each event comes with an event object that contains related information.
How to Capture Click Events
Let's take a look at how to capture this click event. We'll start with a simple Book component. This component will display the name of a book and when clicked, it will log a message to the console.
class Book extends React.Component {
handleClick() {
console.log('You clicked a book!');
}
render() {
return (
<div onClick={this.handleClick}>
{this.props.name}
</div>
);
}
}
Here, we're using the onClick
attribute to register our handleClick
method as the event handler for the click event. When the div is clicked, our handleClick
method is called, and 'You clicked a book!' is logged to the console.
Getting Data from the Clicked Component
But what if we want to log the name of the clicked book instead? That's where the event object comes in handy.
In ReactJS, every event handler receives a synthetic event as an argument. This synthetic event is a cross-browser wrapper around the browser’s native event. It has the same interface as the browser’s native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.
Let's modify our handleClick
method to log the event object:
handleClick(event) {
console.log(event);
}
If you click a book now, you'll see a lot of data logged to the console. This is all the data from the synthetic event. But there's no book name in sight. That's because the book name is not part of the event data – it's part of the component's props.
To log the book name, we'll need to access it from the props:
handleClick(event) {
console.log(this.props.name);
}
Now, when you click a book, its name is logged to the console.
Keeping Context in Class Components
In the above code, we used 'this' to refer to the component instance. But be careful, 'this' inside the handleClick method doesn't always refer to the component instance. By default, event handlers in React lose their context, so 'this' is undefined.
To fix this, you need to bind the context in the constructor of the component:
class Book extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick(event) {
console.log(this.props.name);
}
// ...
}
Now, 'this' inside handleClick
always refers to the component instance, so we can access the props.
Conclusion
ReactJS is a powerful tool for building interactive web applications. Understanding how to capture and handle events is key to leveraging this power. Always remember, every interaction in your application is an opportunity to engage your users. So, the next time a user clicks a component, think of it as them knocking on your door. How you respond could make all the difference in their user experience!