Check if property exists using React.js

前端 未结 6 1747
悲哀的现实
悲哀的现实 2020-12-29 21:42

I\'m new to using react.js, and am trying to write a re-usable component that has an optional property passed to it. In the component, that optional property pulls data from

6条回答
  •  旧时难觅i
    2020-12-29 22:35

    Check if a property exists using React.js

    There are two options you can use. the && operator and If statement to check if the props exist. Option 1 will check if the property exists then run the second part of the code. It works like an if without the if.

    Option 1

    this.props.property && this.props.property
    

    Option 2

    if(this.props.property){
    this.props.property
    }
    

    This also works with function names.

    You can use this also check to render components and tags.

提交回复
热议问题