How to conditionally add or not onClick on a div in react?

前端 未结 6 1366
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 02:26

I would like to know if it is possible to set onClick on a div element in react based on the value of a property in my case canClick.

I am awar

6条回答
  •  说谎
    说谎 (楼主)
    2021-02-01 02:52

    You can add Conditional Operator for this kind of purpose.

    handler(){
    }
    render() {
      const { canClick} = this.state
      return (
          
    { (canClick) ? (
    clickble
    ) : (
    not clickble
    ) }
    ) }

    Here is document example for that reactjs.org/docs/conditional-rendering

提交回复
热议问题