6

i would like to use if statement in the return of render method , what i mean is something like this :

export default class App extends Component 
{

   render()
   {
        return(
             <View>
               if(this.state.error)
               {
                 <View>
                       <Image source={{"error"}} />
                       <Text>An Error Happen</Text>
                   </View>
               }
               else
               {
                   <View>
                       <List dataArray={this.state.items}></List>
                   </View>
                }
              </View>
        );
   }

}

i can use ternary operator , but what i want is to use if statement if it possible

5
  • You cannot use JS inside JSX. You can put this in a helper function and render it in the return method. Commented Jul 20, 2017 at 14:12
  • Read the documentation for React. Commented Jul 20, 2017 at 14:13
  • @Envision good idea , thank for replay Commented Jul 20, 2017 at 14:13
  • why the down vote ? Commented Jul 20, 2017 at 14:14
  • @Ali Because you should start by reading docs :) Commented Jul 20, 2017 at 14:25

5 Answers 5

13

Background

What is the difference between a statement and expression

Specific to JavaScript:

What is the difference between an expression and a statement in JS?

JavaScript: declarations vs expressions vs statements

return expects an expression as the argument. if statements are not expressions (makes sense, since statement is in the name).

The implementation details of JSX (that stuff with <Tags>, but in javascript) is also relevant. JSX is compiled into normal JavaScript.

<MyButton color="blue" shadowSize={2}>
  Click Me
</MyButton>

compiles to

React.createElement(
  MyButton,
  {color: 'blue', shadowSize: 2},
  'Click Me'
)

documentation.

If you want to evaluate a JavaScript expression in JSX you need to enclose it in curly braces.

const myButtonColor = 'blue'
//...
<MyButton color={myButtonColor} shadowSize={2}>
  Click Me
</MyButton>

Back to the Question

Ways of accomplishing if-like functionality:

  1. inline logical and (&&)
  2. ternary operator (?)
  3. return in if/else statements

Let's look at some examples:

Inline Logical And

render() {
  return (
    <View>
      {this.state.error && <Text> There's an error! </Text>}
    </View>
  )
}

This method is the simplest and I recommend trying it first. It makes sense when you don't want to do anything if the condition evaluates to false. && behaves a bit different in javascript than in other languages. When this.state.error evaluates to false, render returns this.state.error, which may have unintended consequences if this.state.error happens to be the empty string or 0.

Ternary Operator

render() {
  return (
    <View>
      {this.state.error ? <Text>Error!</Text> : <Text>No Error!</Text>}
    </View>
  )
}

The biggest problem with this is that the lines get long pretty quickly. You can get around this with parentheses.

render() {
  return (
    <View>
      {this.state.error ? (
        <Text>Long, detailed error message</Text>
      ) : (
        <Text>No Error!</Text>
      )}
    </View>
  )
}

You could alternatively use JavaScript strings declared before return along with this method.

Return in if Block

render() {
  if(this.state.error) {
    return (<Text>Long, detailed error message</Text>)
  }
  else {
    return (<Text>No Error!</Text>)
  }
}

The problem with this method is if that if you have a long render method and a small conditional change, you will have to nearly repeat the render function in each if block. I generally use this method as a last resort.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed it did. I just fixed it.
8

this is the simple one :

render()
   {
        return(
             <View>
               {this.state.error ?
                 (<View>
                       <Image source={{"error"}} />
                       <Text>An Error Happen</Text>
                 </View>)
               :
                 (<View>
                       <List dataArray={this.state.items}></List>
                 </View>)
                }
              </View>
        );
   }

maybe can help you :)

Comments

2

You should try somthing like this:

export default class App extends Component {
render() {
  if(this.state.error)
  {
    return (
     <View>
       <Image source={{"error"}} />
       <Text>An Error Happen</Text>
     </View>
     )
  }
  else {
    return (
      <View>
        <List dataArray={this.state.items}></List>
      </View>
     );
   }
  }
}

Comments

1

You can do this:

 render()
   {
        return(
             <View>
               {this.state.error && 
                 <View>
                       <Image source={{"error"}} />
                       <Text>An Error Happen</Text>
                   </View>
               }
               {!this.state.error &&
                   <View>
                       <List dataArray={this.state.items}></List>
                   </View>
                }
              </View>
        );
   }

2 Comments

what i want to use if , but it seem to be not possible
you can not use if within a return block
1

I suggest to take advantage of js in this case:

{ this.state.error &&
   <View>
    <Text>show error</Text>
   </View>
}
{ !this.state.error &&
   <View>
    <Text>all good</Text>
   </View>
}

It can look better than ternary in some cases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.