How to change states with onClick event in ReactJS using functional components ?
Last Updated :
02 Nov, 2023
In modern web applications, to change states with onClick event in React JS using functional components is done by Hooks in React as the functional components don't have inbuilt states like class components. With Hooks, state objects are completely independent of each other, so you can have as many state objects as you want. In a single-page application, the useState hook is the best way to simply change the state with a click without reloading the entire page.
Prerequisites
Approach
Using useState hook to dynamically change the states with onClick event in react js. When clicking on the button the state for the component to be rendered is set to true and other to false.
Steps to create React Application:
Step 1: Create a React application using the following command.
npx create-react-app projectname
Step 2: After creating your project folder i.e. projectname, move to it using the following command.
cd projectname
Project Structure

Example: Switching components with onClick event to update state to true or false using useState hook.
JavaScript
// Filename - App.js
import ComponentOne from "./components/ComponentOne";
import "./App.css";
function App() {
return (
<div className="App">
<h1 className="geeks">GeeksforGeeks</h1>
<h3>
Change States with onClick Event in React JS
</h3>
<ComponentOne />
</div>
);
}
export default App;
JavaScript
// Filename - components/ComponentOne.js
import { useState } from "react";
function ComponentOne() {
const [description, setDescription] = useState(true);
const [reviews, setReviews] = useState(false);
const descriptionHandler = () => {
setDescription(true);
setReviews(false);
};
const reviewsHandler = () => {
setDescription(false);
setReviews(true);
};
return (
<div>
<button onClick={descriptionHandler}>
Descriptions
</button>
<button onClick={reviewsHandler}>
Reviews
</button>
<div className="container">
{description && (
<div>
<p>
This is a <b> descriptive </b>
component
</p>
<h3>Descriptions:</h3>
</div>
)}
{reviews && (
<div>
<p>
This is a <b> Review </b>
component
<br />
</p>
<h3>Reviews:</h3>
</div>
)}
</div>
</div>
);
}
export default ComponentOne;
CSS
/* Filename - App.js */
.App {
text-align: center;
}
.geeks {
color: green;
}
.container {
width: 20rem;
margin: auto;
box-shadow: 0px 5px 10px gray;
border-radius: 15px;
padding: 3%;
}
button {
font-size: large;
margin: 2%;
background-color: green;
color: white;
border-radius: 5px;
padding: 10px;
}
Step to run the application: Run the application using the following command:
npm start
Output: Now, open the URL http://localhost:3000/, and you will see the following output.

Similar Reads
Advantages of using Functional Components with React Hooks In the past, React mainly used class-based components as its building blocks. However, functional components, which are essentially JavaScript functions that receive props (inputs) and return JSX (rendered content), were limited in their capabilities. With React 16.8 and the introduction of hooks, f
4 min read
How to convert functional component to class component in ReactJS ? ReactJS offers two primary ways of creating components: functional components and class components. While functional components are more concise and easier to reason about, there are situations where class components may be necessary with the flexibility to use lifecycle methods and manage the state
2 min read
How to Send state/props to Another Component in React with onClick? The props and state are the main concepts of React. Actually, only changes in props and/ or state trigger React to re-render your components and potentially update the DOM in the browserProps are used to pass data from a parent to a child component.State is used to manage dynamic data within a compo
3 min read
How To Get Previous State In ReactJS Functional Component? To get the previous state in ReactJS Functional Component we can use the previous value of the state while using the set state method. State can only be directly accessed in the class component so we will be using the react useState hook to accomplish the task.PrerequisitesReact JSReact JS functiona
2 min read
How to create a Functional Component in React? To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
2 min read