How do you conditionally pass a parameter to a component in React?
-
How to conditionally pass a parameter to a component in "React"? I thought that something like this could turn out, but this code crashes with an error:
<Page title="SOME TEXT" {this.state.content === null ? "no_text" : null}>
You need to make sure that if this.state.content is equal to null , then
& lt; Page title = "SOME TEXT" no_text & gt;
If this.state.content is equal to anything else, then
& lt; Page title = "SOME TEXT" & gt;
JavaScript Lilah Armstrong, Feb 4, 2020 -
<Page title="SOME TEXT" no_text={this.state.content === null}>
UPD:
<Page title="SOME TEXT" {...(this.state.content === null?{no_text:true}:{})}>
Owen Griffin -
As I understand it, no_text is a boolean parameter (true or false).
Can be done like this:
& lt; Page title = '...' no_text = {!! this.state.content} & gt;
!! value - converts value to a boolean value:
!!null === false
!!'' === false
!![] === true
!!'aaa' === true
Perhaps this is a typo or not the entire component, but it is written like this
& lt; Page / & gt; or & lt; Page & gt; & lt; / Page & gt; and you have & lt; Page & gt;
Anonymous
2 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!