How To Enable Button Based On If statement in ReactJS?
Last Updated :
03 Oct, 2024
React.js is one of the most popular JavaScript libraries for building user interfaces, and one common task is enabling or disabling a button based on certain conditions or user inputs. This is often achieved by using conditional logic such as if statements.
In this article, we'll explore how to enable a button based on an if statement in React.js. We'll break it down step by step and include a practical example to help you understand the process.
Steps To Enable Button Based On If Statement
In order to display the button conditionally using the if and else statements, we can use the state in react.js. Declare the state in the constructor method because it loads first when the component is loaded. In order to toggle between user and admin, we need to use an event handler. Using this event handler, we can toggle the state of the user. Below is the implementation of the code for displaying it.
Setting up Your React Project
Before we dive into the code, make sure you have a React project set up. If you haven't created a project yet, you can do so using create-react-app by following these steps:
npx create-react-app react-gfg
cd react-gfg
Folder Structure
Folder StructureDependencies
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example:
JavaScript
//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
// import * as serviceWorker from './serviceWorker';
import Demo from './Demo'
ReactDOM.render(
<React.StrictMode>
<Demo />
</React.StrictMode>,
document.getElementById('root')
);
// serviceWorker.unregister();
JavaScript
//Demo.js
import React, { Component } from 'react'
class DemoUser extends Component {
constructor() {
super()
this.state = {
isAdmin: true
}
this.toggleState = this.toggleState.bind(this);
}
toggleState() {
this.setState({
isAdmin: !this.state.isAdmin
}
)
}
render() {
if (this.state.isAdmin) {
return (
<div>
<h3> Welcome Admin </h3><span >
Is the user admin :
{this.state.isAdmin.toString()}</span>
<br />
<button onClick={this.toggleState}>
Toggle between user and admin
</button>
</div>
)
}
else {
return (
<div>
<h3> Welcome User </h3><span >
Is the user admin :
{this.state.isAdmin.toString()}</span>
<br />
<button onClick={this.toggleState}>
Toggle between user and admin
</button>
</div>
)
}
}
}
export default DemoUser
To start the application run the following command.
npm start
Output:
Enable Button Based On If statement in ReactJS
Similar Reads
How to Change Button Text on Click in ReactJS ? In this article, we will learn about diverse methods for altering button text dynamically upon clicking on the button. React, a widely embraced JavaScript library for crafting user interfaces provides several approaches to seamlessly achieve this functionality. The key lies in manipulating the compo
3 min read
How to Disable a Button in React JS ? Disabling a button in React JS means making it unclickable and visually indicating its unavailability. This is done by setting the button's disabled attribute to true. It's used for controlled interactions, like form submissions with valid data.A disabled button becomes unclickable and visually sign
4 min read
How to create a basic button in React Native ? In this article, we will learn how to create a basic button in React Native. To build a React Native app, we will use the Expo CLI, which provides a smoother experience for running your applications.ExpoIt is a framework and a platform for universal React applications. It is a set of tools and servi
5 min read
How to create Bluetooth toggle button in ReactJS? The Bluetooth Toggle button is a component in Material UI for React that allows users to easily turn Bluetooth on and off. Integrating this component into a ReactJS application is straightforward, and the following approach can be employed.Prerequisites:NodeJS or NPMReact JSMaterial UIApproach:Manag
2 min read
How to Create Button in React-Native App ? React Native provides pre-defined components like button and TouchableOpacity to create buttons in a react native app. In this article, we will see how to create buttons in react-native, their syntax, and different types of buttons available in react-native.Table of ContentApproachButton in React Na
4 min read