if-else statement inside jsx: ReactJS

前端 未结 12 1041
广开言路
广开言路 2020-11-22 08:48

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

For example:

render() {
    return (   
        

        
相关标签:
12条回答
  • 2020-11-22 09:22

    What about switch case instead of if-else

      render() {
        switch (this.state.route) {
          case 'loginRoute':
            return (
                <Login changeRoute={this.changeRoute}
                  changeName={this.changeName}
                  changeRole={this.changeRole} />
            );
          case 'adminRoute':
            return (
                <DashboardAdmin
                  role={this.state.role}
                  name={this.state.name}
                  changeRoute={this.changeRoute}
                />
            );
          default: 
            return <></>;
        }
    
    0 讨论(0)
  • 2020-11-22 09:24
     render() {
         return (   
             <View style={styles.container}>
             (() => {                                                       
                 if (this.state == 'news') {
                    return  <Text>data</Text>
                }
                else 
                 return  <Text></Text>
            })()
             </View>
         )
     }
    

    https://react-cn.github.io/react/tips/if-else-in-JSX.html

    0 讨论(0)
  • 2020-11-22 09:26

    I find this way is the nicest:

    {this.state.yourVariable === 'news' && <Text>{data}<Text/>}
    
    0 讨论(0)
  • 2020-11-22 09:29

    I do like this and its working fine.

    constructor() {
       super();
    
       this.state ={
         status:true
       }
    }
    
    render() {
       return( 
    
         { this.state.status === true ?
               <TouchableHighlight onPress={()=>this.hideView()}>
                 <View style={styles.optionView}>
                   <Text>Ok Fine :)</Text>
                 </View>
              </TouchableHighlight>
               :
               <Text>Ok Fine.</Text>
         }
      );
    }
    
    hideView(){
      this.setState({
        home:!this.state.status
      });
    }
    
    0 讨论(0)
  • 2020-11-22 09:35

    You can't provide if-else condition in the return block, make use of ternary block, also this.state will be an object, you shouldn't be comparing it with a value, see which state value you want to check, also return returns only one element, make sure to wrap them in a View

    render() {
        return (
          <View style={styles.container}>
          {this.state.page === 'news'? <Text>data</Text>: null}
          </View>
    
         )
    }
    
    0 讨论(0)
  • 2020-11-22 09:35

    For this we can use ternary operator or if there is only one condition then "&&" operator .Like this:-

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

    }

    //This is only for if or only for one condition
    
    render() {
    
    return (   
        <View style={styles.container}>
          {this.state == 'news') &&
               <Text>data</Text>
            }
        </View>
    )
    

    }

    0 讨论(0)
提交回复
热议问题