if-else statement inside jsx: ReactJS

前端 未结 12 1159
广开言路
广开言路 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: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 (
          
          {this.state.page === 'news'? data: null}
          
    
         )
    }
    

提交回复
热议问题