简体   繁体   中英

Jquery to react.js

I want to convert these few lines of Jquery to a react code. Everything that I try leads to so many errors.

$(".info-item .btn").click(function(){
  $(".container").toggleClass("log-in");
});
$(".container-form.btn").click(function(){
  $(".container").addClass("active");
});

Don't truthfully know if you mean you just want the logic of it transferred over to React as you're new to React but i gave it a shot based on what I think you were asking

class Example extends Component {
  constructor(){
    super();
    this.state = {
      buttonTouched: false
    }
  }

    handleClick = () => {
      this.setState(prevState => ({
        buttonTouched: !prevState.buttonTouched
      }));
    }

    render(){
      return(
        <div>
          <button onClick={this.handleClick} className="container-form btn">Click me</button>
          <div className={this.state.buttonTouched ? "container active" : "container" }></div>
        </div>
      )
    }
}

Didn't actually test this but this should work for your second question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM