0

I am trying to have a button to change its color when clicked in React.

I found this and tested it: https://reactgo.com/react-change-button-color-onclick/

The code being:

const [active, setActive] = useState(false);

......

<button
  onClick={() => {
    setActive(!active)
  }}
  style={{
    backgroundColor: active ? "black" : "white"
  }}
>
  The-Button
</button>

Though it works, it is not what I need at the moment.

With this code the button goes white, then black, then white, then black, .... at each click.

But what I want is the button being black going white when clicked and back to black when released.

How can I get this effect?

4 Answers 4

1

You don't need React state or any special click handler logic to achieve toggling the background color while the button is pressed. You can use pure CSS to do this.

Add a class to the button that provides the default non-pressed CSS styling, and then provide also :active styling for when the button is pressed.

Example:

<button className="the-button">The Button</button>
.the-button {
  background-color: black;
  color: white;
  .....
}

.the-button:active {
  background-color: white;
  color: black;
}

default non-pressed pressed

Sign up to request clarification or add additional context in comments.

2 Comments

This works even better than the other answers. The other solution worked on my computer but not on my mobile. This one does.
@Michel That is likely because touchscreen devices such as mobile phones and tablets don't generally use mice so there are not any mouse events. You might be able to leverage pointer events which are intended to "handle pointing input devices such as a mouse, pen/stylus or touch", so a kind of mix of mouse and touch events. That said, I think solving it in CSS land is more optimal.
1

For this need to use mouse events

<button
    onMouseDown={() => setActive(true)}
    onMouseUp={() => setActive(false)}
    onMouseLeave={() => setActive(false)}
    style={{ backgroundColor: active ? 'white' : 'black', color: active ? 'black' : 'white' }}
>
    The-Button
</button>

Comments

0

To achieve this, you will need to make use of these events to toggle between colors: onMouseDown onMouseUp onMouseLeave

...

 <button
    onMouseDown={() => setActive(true)}
    onMouseUp={() => setActive(false)}
    onMouseLeave={() => setActive(false)}
    style={{
      backgroundColor: active ? "white" : "black",
      color: active ? "black" : "white",
      }}
    >
     The-Button
 </button>
...

4 Comments

Other than some links to documentation this is identical to an existing answer.
The page only refreshed after I submitted my answer. I wouldn't have proceeded if I had seen a similar answer.
Yet you deleted it after posting, but then undeleted it for some reason in spite of it being a duplicate answer.
Well, I figured mine has links to provide more context. Besides, the focus here is to solve the issue.
0

You can try like his

<button
    onMouseDown={() => setActive(true)}
    onMouseUp={() => setActive(false)}
    onMouseLeave={() => setActive(false)}
    style={{ 
         backgroundColor: active ? '#fff' : '#000', 
         color: active ? '#000' : 'fff'
    }}
>
    Button Label
</button>

1 Comment

This is no different than two other existing answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.