Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if-else statement inside jsx: ReactJS

I need to change render function and run some sub render function when a specific state given,

For example:

render() {     return (            <View style={styles.container}>             if (this.state == 'news'){                 return (                     <Text>data</Text>                 )             }         </View>     ) } 

How can I implement that without changing scenes, I will use tabs to change content dynamically.

like image 291
user8026867 Avatar asked May 18 '17 11:05

user8026867


People also ask

Can we use if-else inside JSX?

if-else statements don't work inside JSX. This is because JSX is just syntactic sugar for function calls and object construction.

How add if-else condition in react JS?

Inline If with Logical && Operator Therefore, if the condition is true , the element right after && will appear in the output. If it is false , React will ignore and skip it. Note that returning a falsy expression will still cause the element after && to be skipped but will return the falsy expression.

Is JSX can be used inside if and for loop?

You can basically use the same structure when working with JSX: var rows = []; for (var i = 0; i < numrows; i++) { // note: we are adding a key prop here to allow react to uniquely identify each // element in this array.


2 Answers

As per DOC:

if-else statements don't work inside JSX. This is because JSX is just syntactic sugar for function calls and object construction.

Basic Rule:

JSX is fundamentally syntactic sugar. After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects. We can embed any JavaScript expression in JSX by wrapping it in curly braces.

But only expressions not statements, means directly we can not put any statement (if-else/switch/for) inside JSX.


If you want to render the element conditionally then use ternary operator, like this:

render() {     return (            <View style={styles.container}>             {this.state.value == 'news'? <Text>data</Text>: null }         </View>     ) } 

Another option is, call a function from jsx and put all the if-else logic inside that, like this:

renderElement(){    if(this.state.value == 'news')       return <Text>data</Text>;    return null; }  render() {     return (            <View style={styles.container}>             { this.renderElement() }         </View>     ) } 
like image 141
Mayank Shukla Avatar answered Sep 29 '22 11:09

Mayank Shukla


You can achieve what you are saying by using Immediately Invoked Function Expression (IIFE)

render() {     return (            <View style={styles.container}>             {(() => {               if (this.state == 'news'){                   return (                       <Text>data</Text>                   )               }                              return null;             })()}         </View>     ) } 

Here is the working example:

Edit awesome-yalow-eb2nu

But, In your case, you can stick with the ternary operator

like image 34
Yusufbek Avatar answered Sep 29 '22 13:09

Yusufbek