How to increase values of different counters when i click different buttons in ReactJS
Introduction to ReactJS
Let's start this journey by understanding what ReactJS is. ReactJS, often referred to as React, is a JavaScript library used for building user interfaces, specifically for single-page applications. You can think of it like the set of building blocks (which we call 'components') you used to play with as a child. Each block or component can be used individually or combined to create a more complex structure (your application).
Beginning with ReactJS
Assuming you already have a basic understanding of JavaScript, let's dive into how we can use ReactJS to build a simple application. In this blog, we'll focus on creating a counter application with multiple buttons, where each button click increases the value of a different counter.
Setting Up Your React Environment
Before you start coding, you need to have Node.js and npm (Node Package Manager) installed on your computer. Think of Node.js as a JavaScript-running environment and npm as a tool that manages all your JavaScript libraries (like React).
Once you have Node.js and npm installed, you can create a new React application by running the following command in your terminal:
npx create-react-app counter-app
This command creates a new directory named counter-app
with all the necessary files and dependencies for a React application.
Understanding the React Component
In our counter application, each counter will be a React component. A component in React is like a function that returns a piece of the user interface (UI). This function can take inputs, called 'props', and return what should be displayed on the screen.
A simple counter component in React could look like this:
import React from 'react';
class Counter extends React.Component {
render() {
return (
<div>
<h1>Counter: 0</h1>
<button>Increment</button>
</div>
);
}
}
In the example above, our Counter
component displays a heading with the text "Counter: 0" and a button labeled "Increment". But right now, clicking the button doesn't do anything. Let's change that.
Adding State to the Counter Component
In React, 'state' is a way of holding, managing and using data that can change over time and affect what's displayed on the screen. Think of it like a box where you keep items (data) that can be changed and used when needed.
To make our counter work, we need to introduce state in our Counter
component. We'll store the current value of the counter in the state and update it when the button is clicked.
import React from 'react';
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
incrementCount = () => {
this.setState({
count: this.state.count + 1
});
}
render() {
return (
<div>
<h1>Counter: {this.state.count}</h1>
<button onClick={this.incrementCount}>Increment</button>
</div>
);
}
}
In the code above, we first initialize the state with a count
of 0 in the constructor
method. Then, we define a method incrementCount
which updates the count
in the state by adding 1 to the current value. We then attach this method to the button's click event using the onClick
attribute.
Now, every time you click the "Increment" button, the value of count
in the state increases by 1, and the new value is displayed on the screen.
Creating Multiple Counters
Now that we have a single counter working, let's create multiple counters. We'll do this by creating several instances of the Counter
component. Each instance will have its own state and therefore its own count.
import React from 'react';
import Counter from './Counter';
class App extends React.Component {
render() {
return (
<div>
<Counter />
<Counter />
<Counter />
</div>
);
}
}
export default App;
In the App
component above, we create three Counter
components. Each Counter
component maintains its own state, so clicking the "Increment" button in one counter won't affect the others.
Wrapping Up
In this blog post, we've seen how to create multiple counters in React, each with its own state. We've learned about React components, state, and event handling. Hopefully, this gives you a good starting point for understanding how data and events are managed in React.
Remember, React is all about components and state. Components define the structure and behavior of your UI, while state holds and manages the data that can change over time and affect what's displayed.
Keep practicing and experimenting with different components and states. With time and experience, you'll find React to be a powerful tool for building dynamic and interactive web applications. Happy coding!