reactjs - How to onclick method set for only one input? -


first please click ss.

right have 2 input has value credit-card , paypal.

i set onclick event creditcard provide card informations.it works fine problem is:

card details doesn`t disappear when click paypal input. works if click creditcart input again. want make disappear click paypal input. mean card details should seem clicking credit card input.

class creditcart extends react.component { constructor(props) {     super(props);     this.state = {show:false};      this.handleclick = this.handleclick.bind(this); }  handleclick () {     this.setstate({ show : !this.state.show})   }    render () {     return (      //2 input here credir-cart has onclick          {this.state.show && <creditcart/>     } 

second component includes cart information part:

class creditcart extends react.component { constructor(props) {     super(props); } render () { // information part } 

your handleclick method wrong. setstate method asynchronous , try execute in batches means previous value might not updated yet.

change to

 handleclick() {      this.setstate(function (prevstate, props) {          return {              show: !prevstate.show          };      });  } 

see state updates may asynchronous


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -