How to Create a Button in React

React is a popular JavaScript library used for building user interfaces. It provides developers with a component-based approach to building web applications, making it easy to create interactive and dynamic UI elements. One common UI element used in almost every web application is a button. In this article, we will explore how to create a button in React and make it respond to user interactions.

Prerequisites

Before we dive into creating a button in React, make sure you have the following prerequisites:

  • Basic knowledge of HTML, CSS, and JavaScript
  • Node.js installed on your machine
  • Familiarity with React and its basic concepts

If you are new to React, I recommend going through the official React documentation and understanding the fundamental concepts before proceeding.

Setting Up a React Project

To create a React project, you can use the create-react-app command-line tool. Open your terminal and run the following command:

npx create-react-app button-demo

This command will create a new directory called button-demo with a basic React project structure. Navigate to the project directory by running:

cd button-demo

Creating a Button Component

In React, UI elements are represented as components. Let’s create a Button component that will serve as our button element. Open the src directory in your project and create a new file called Button.js. Inside the file, add the following code:

import React from 'react';

const Button = () => {
  return (
    <button>
      Click me!
    </button>
  );
}

export default Button;

In the code above, we import the necessary dependencies and define a functional component called Button. The component returns a button element with the text “Click me!”.

Using the Button Component

Now that we have our Button component, let’s use it in our main application. Open the src directory and locate the App.js file. Replace its content with the following code:

import React from 'react';
import Button from './Button';

const App = () => {
  return (
    <div>
      <h1>Welcome to My Button Demo</h1>
      <Button />
    </div>
  );
}

export default App;

In the code above, we import the Button component we created earlier and include it in the App component. We also add a heading to provide a title for our demo.

Running the Application

To see our button in action, go back to your terminal and run the following command:

npm start

This command will start the development server and open your application in a web browser. You should see the heading “Welcome to My Button Demo” and a button below it.

Customizing the Button

To make the button more interactive and responsive, we can add some additional features. Let’s modify our Button component to include an onClick event handler and some CSS styling.

Update the Button.js file with the following code:

import React, { useState } from 'react';
import './Button.css';

const Button = () => {
  const [clicked, setClicked] = useState(false);

  const handleClick = () => {
    setClicked(!clicked);
  }

  return (
    <button className={clicked ? 'clicked' : ''} onClick={handleClick}>
      {clicked ? 'Clicked!' : 'Click me!'}
    </button>
  );
}

export default Button;

In the code above, we import the useState hook from React to manage the state of our button. We define a clicked state variable and a handleClick function that toggles the state when the button is clicked. The className property of the button is dynamically updated based on the clicked state, allowing us to apply different styles.

Create a new file called Button.css in the src directory and add the following CSS:

.clicked {
  background-color: #f00;
  color: #fff;
}

This CSS code sets the background color to red and the text color to white when the button is clicked.

Conclusion

Congratulations! You have successfully created a button component in React. We covered the basics of creating a button component, using it in an application, and adding interactivity. You can further customize the button by exploring CSS styles, adding additional event handlers, or passing props to the component.

Remember to continue exploring React’s documentation and practice building more complex components to enhance your skills. Happy coding!

author's bio photo

Hi there! I am Avic Ndugu.

I have published 100+ blog posts on HTML, CSS, Javascript, React and other related topics. When I am not writing, I enjoy reading, hiking and listening to podcasts.