I'm trying to pass a prop to a relative child in react, only if it meets certain conditions.
I know that you can make a type of conditional to send props like this:
<SomeComponent
ConditionalProp={
(this.state.exampleData === 'some string') ? this.state.exampleData : null
}
/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
But I need to do more validations, so far this is what I have:
<MyComponent
ConditionalProps={
(this.props.channel && this.props.some) ?
(this.props.some.conv && (this.props.some.conv.id === this.props.channel.conv.id) ?
this.props.channel :''
(this.state.contentId === this.props.channel)? this.props.channel:''
): null
}
/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Let me explain, what I want to do is: if channel and some exist, two possible cases will be validated: if some.conv exists and also some.conv.id is === channel.conv.id? then it must be passed as prop channel.
If that is false and contentId exists and also is exactly equal to channel then channel is passed.
If neither of those cases is true, a null or an empty string must be passed '' (I have no problem with either of the two)
I am not very sure that what I did is correct and what I am sure is that it is not the best way to carry it out