I have been encountering with this conditional operator,
phone={this.props.projectDetails?.agency?.phone ?? this.props.projectDetails.phone }
I wanted to use Phone number that is given in agency?.phone but at some point we didn’t had phone number in agency so we were using projectDetail's phone so overcome this issue.
CodePudding user response:
Nullish coalescing operator means If is null or undefined you will render projectDetails.phone prop
CodePudding user response:
In case if we don't have any else condition , use Nullish coalescing operator (??)
this.props.projectDetails?.agency?.phone (if it has value) ?? (then show) this.props.projectDetails.phone
.. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing
CodePudding user response:
it's Nullish coalescing operator and in your case exactly equal to this :
phone={this.props.projectDetails?.agency?.phone ? this.props.projectDetails.agency.phone : this.props.projectDetails.phone }