Home > Mobile >  How to check if a React component has a certain attribute?
How to check if a React component has a certain attribute?

Time:10-07

Is there any way to check if a React component has a certain attribute?

CodePudding user response:

If you're talking about a component's props, you can use:

if (this.props.<your prop> != undefined) { *your logic here* }

CodePudding user response:

If you are in the parent component, there is NO way to know if a pops is there in the child comp or not.

And if you want to know current component's prop:

1- in class based component: this.props.testProp

2- in functional component: props.testProp

for example:

{this.props.testProp !== undefined ? <a href="#">link</a> : null}

or

{props.propInQuestion !== undefined ? <a href="#">link</a> : null}
  • Related