Search code examples
javascriptreactjssemantic-ui

Prevent multiple button color activation in ReactJS


I have two buttons with ReactJS and SemanticUI. When each one is clicked, the background color changes. I would like to have only one button activated at a time, meaning that if the red one is clicked, the green one deactivates and vice-versa, taking back the default white background color. Right now both can be clicked with the color change at the same time.

Here's my component:

export class BoutonRefus extends Component {
  constructor(props) {
    super(props);
    this.state = {
      button: true
    };
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState({
      button: !this.state.button
    });
  }
  render() {
    return (
      <>
        <div
          className={
            this.state.button
              ? "ui button medium refus true"
              : "ui button medium refus false"
          }
          onClick={this.handleClick}
        ></div>
      </>
    );
  }
}

export class BoutonAccepte extends Component {
  constructor(props) {
    super(props);
    this.state = {
      button: true
    };
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState({
      button: !this.state.button
    });
  }
  render() {
    return (
      <>
        <div
          className={
            this.state.button
              ? "ui button medium accepte true"
              : "ui button medium accepte false" && 
          }
          onClick={this.handleClick}
        ></div>
      </>
    );
  }
}

This component is called here:

boutonAccepter(t) {
        return (
            <>
            <BoutonAccepte 
            className="ui button medium accepte true"
            onClick={() => {this.voter(true)}}
            text={t('flot.split.vote.accepter')}
                />
            </>
        )
    }

boutonRefuser(t) {
        return (
            <>
            <BoutonRefus 
            className="ui button medium refus true"
            onClick={() => {
                this.justifierRefus()
                this.voter(false)
            }}
            text={t('flot.split.vote.refuser')}
            />
            </>
        )
    }


Solution

  • You don't need a class component for that. Just use useState() hook for your local state and make a functional component. Also, making different components for each button seems redundant. Try this, just paste your classnames ofcourse :)

      function Buttons(){
       const [selectedButton, setSelectedButton] = React.useState();
    
       return (
          <div>
              <button type="button" 
                 className={selectedButton === "refuse" ? "active-classname" : "inactive-classname"}
                 onClick={() => setSelectedButton("refuse")}>Refus</button>
              <button type="button" 
                 className={selectedButton === "refuse" ? "active-classname" : "inactive-classname"}
                 onClick={() => setSelectedButton("refuse")}>Accepte</button>
          </div>
       ); 
    }