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

前端 未结 6 1363
爱一瞬间的悲伤
爱一瞬间的悲伤 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 03:00

    Put the condition like this:

    onClick={canClick ? this.handler : undefined }
    

    Working Code:

    class App extends React.Component {
      
       _click(){
          // it will not print the value
          console.log('yes');
       }
    
       render(){
          return (
            
    Click
    ) } } ReactDOM.render(, document.body);
    
    

提交回复
热议问题