Categories
React Answers

How to add active class to button with React?

Spread the love

To add active class to button with React, we set the className of the button.

For instance, we write

<div>
  {buttons.map((name, index) => {
    return (
      <input
        type="button"
        className={active === name ? "active" : ""}
        value={name}
        onClick={() => someFunct(name)}
        key={name}
      />
    );
  })}
</div>

to add buttons by adding inputs with type button.

Then we apply the active class if the active value equals name.

When do something with name when we click the button

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *