【问题标题】:How to use write ternary operator for click event in React.js如何在 React.js 中使用写入三元运算符进行点击事件
【发布时间】:2017-09-27 08:47:14
【问题描述】:

我在我的应用程序中使用了几个三元运算符,但我不知道如何将三元运算符用于点击事件。

我正在制作一个弹出菜单。一旦该菜单项已被选中,其中一个菜单项将不可点击。只有另一个菜单项应该是可点击的。

图片

我尝试了以下代码,但没有成功。我们不能在 React 中使用三元运算符来处理点击事件吗?

<p id="menuItem" {!this.sate.priceBar? onClick={this.clickHandle} : "" } style={!this.state.priceBar? { color:'white'} : {color : '#BBBBBB'} }>{!this.state.priceBar? "Spent Last 14 Days" : "Spent Last 14 Days"}</p>

完整代码

class Home extends React.Component{

constructor(props){
    super(props);
    this.state = {
        slideOpen : false,
        priceBar: false,
        open: false,
    }
this.handleClick = this.handleClick.bind(this);
this.clickHandle = this.clickHandle.bind(this);
}


 handleClick() {
    this.setState({
        slideOpen : !this.state.slideOpen
    })
 console.log("slideOpen" + !this.state.slideOpen)
 }

 clickHandle() {

 this.setState({ 
          priceBar : !this.state.priceBar,
          open: false
  })
 console.log(!this.state.priceBar)
 }

 handleTouchTap = (event) => {
 // This prevents ghost click.
 event.preventDefault();

 this.setState({
  open: true,
  anchorEl: event.currentTarget,
 });
};

handleRequestClose = () => {
this.setState({
  open: false,
});
};

render(){
  const PaymentPanel = this.state.slideOpen? "slideOpen" : "";
  const Dropdown = this.state.open? "show" : "";

return(
<div>
<div id="PaymentPanel" className={PaymentPanel} >
<div id="PaymentTitle" >{!this.state.priceBar? "Spent Last 14 Days" : "Spent Last 30 Days"}<img src={PaymentArrow} className="PaymentArrow PaymentToggle" onClick={this.handleTouchTap}/></div>
<div id="Dropdown" className={Dropdown}  open={this.state.open}>
<p className="popoverToggle" onClick={this.handleRequestClose}> </p>
<p id="menuItem" onClick={this.clickHandle} style={!this.state.priceBar? { color:'white'} : {color : '#BBBBBB'} }>{!this.state.priceBar? "Spent Last 14 Days" : "Spent Last 14 Days"}</p>
<p id="menuItem" onClick={this.clickHandle} style={this.state.priceBar? { color:'white'} : {color : '#BBBBBB'} }>{this.state.priceBar? "Spent Last 30 Days" : "Spent Last 30 Days"}</p>
</div>


<h2 id="paymentSum" className={!this.state.open? "" : "close"}>{!this.state.priceBar? "$9,964.55" : "$19,929.1"}</h2>


<ul className="paymentTool">
<li>
<div onClick={this.handleTouchTap} className="tool">VISA <br />  {!this.state.priceBar? "$9,504.13" : "$19,008.26"}</div></li>
<li><div className="tool">MasterCard <br />   {!this.state.priceBar? "$490.64" : "$981.28"}</div></li>
<li><div className="tool">PayPal  <br /> {!this.state.priceBar? "$824.52" : "$1,649.04"}</div></li>
</ul>
<div className="paymentSlide" onClick={this.handleClick}>{this.state.slideOpen? <img src={Arrowup} alt="arrowup" className="arrowup" /> : <img src={Arrowdown} alt="arrowdown" className="arrowdown"/>}</div>
</div>

 <div className="PaymentTable" >
 <PaymentTable />
<ul>
</ul>
</div>
</div>
    )
}
}

【问题讨论】:

  • !this.sate.priceBar 这里state 是错字吧?
  • @ShubhamJain 没有。我使用状态来检查条件。

标签: reactjs


【解决方案1】:

如果你在 React 中使用函数而不是类:

onClick={() => condition ?  functionToClick() : null}

【讨论】:

    【解决方案2】:

    当然,三元操作不能用于返回dom的属性,因为表达式的返回值是字符串,浏览器和react.js引擎不会把字符串当作属性。

    改为:

    onClick={!this.state.priceBar ? this.clickHandle : ''}
    

    修改你的代码:

    handleTouchTap(event){
    
    }
    
    handleRequestClose() {
    }
    

    class Home extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            slideOpen : false,
            priceBar: false,
            open: false,
        }
       
    this.handleClick = this.handleClick.bind(this);
    this.clickHandle = this.clickHandle.bind(this);
    }
    
    
     handleClick() {
        this.setState({
            slideOpen : !this.state.slideOpen
        })
     console.log("slideOpen" + !this.state.slideOpen)
     }
    
     clickHandle() {
    
     this.setState({ 
              priceBar : !this.state.priceBar,
              open: false
      })
     console.log(!this.state.priceBar)
     }
    
     handleTouchTap (event) {
     // This prevents ghost click.
     event.preventDefault();
    
     this.setState({
      open: true,
      anchorEl: event.currentTarget,
     });
    }
    
    handleRequestClose () {
    this.setState({
      open: false,
    });
    }
    
    render(){
      const PaymentPanel = this.state.slideOpen? "slideOpen" : "";
      const Dropdown = this.state.open? "show" : "";
    
    return(
    <div>
    <div id="PaymentPanel" className={PaymentPanel} >
    <div id="PaymentTitle" >{!this.state.priceBar? "Spent Last 14 Days" : "Spent Last 30 Days"}</div>
    <p id="menuItem" onClick={this.clickHandle} style={!this.state.priceBar? { color:'white'} : {color : '#BBBBBB'} }>{!this.state.priceBar? "Spent Last 14 Days" : "Spent Last 14 Days"} (Click me!)</p>
    
    click top line
    
    
    </div>
    </div>
    
        )
    }
    }
    ReactDOM.render(<Home />, document.querySelector('.app'))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    <div class="app"></div>

    【讨论】:

    • 你的代码存在一些错误,我修改了,它可以工作。
    【解决方案3】:
    <p id="menuItem" onClick={!this.state.priceBar ? this.clickHandle : null}>
    

    【讨论】:

    • 这是一个答案吗?如果有,请补充说明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 2015-09-15
    • 2021-08-14
    • 1970-01-01
    相关资源
    最近更新 更多