Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在render内执行if语句中的内容_Reactjs_React Native_If Statement - Fatal编程技术网

Reactjs 在render内执行if语句中的内容

Reactjs 在render内执行if语句中的内容,reactjs,react-native,if-statement,Reactjs,React Native,If Statement,我想执行if语句中的所有行,但我不能 这是我的密码 import React, { Component } from 'react'; import { Text, View } from 'react-native'; class test extends Component { constructor() { super(); this.state = { data: [] }; } testFunction = () => { return (

我想执行if语句中的所有行,但我不能

这是我的密码

import React, { Component } from 'react';
import { Text, View } from 'react-native';
class test extends Component {
  constructor() {
    super();
    this.state = { data: [] };
  }
  testFunction = () => {
    return (
      <View>
        <Text>Anything</Text>
      </View>
    );
  };

  render() {
    return (
      <View>{data.length > 0 ? <Text>Hi</Text> : this.testFunction() <Text>Hello</Text>}</View>
    );
  }
}
export default test;
我想执行这个.test函数,您好


谢谢你

你可以这样做:

渲染{ //数据是否已初始化? 如果data.length>0 回来 你好 其他的 回来 {this.testFunction} 你好
} 您可以修改函数,使其返回组件

 condition1 () {
  // do something 
  return ( <FlatList
    data={[{key: 'a'}, {key: 'b'}]}
    renderItem={({item}) => <Text>{item.key}</Text>}
  />);
 }

 condition2 () {
  // do something 
  return ( <Text>Hello</Text>);
 }
在渲染中调用它

render() {
 return (
  <View> { data.length > 0 ? this.condition1() : this.condition2()} </View>
)}

请提供完整的组件代码Simport React,{component}来自“React”;从“react native”导入{Text,View};类测试扩展组件{constructor{super;this.state={data:[],}}}testFunction=>{return Anything}render{return{data.length>0?Hi:this.testFunction Hello};}导出默认测试;我想在Hi之后和之前添加一些内容:您可以将它们全部放在另一个函数中。请参阅我修改的答案。我有一个问题..我在函数中使用FlatList但FlatList不显示什么是FlatList不显示?在函数中添加了一个FlatList组件。请参阅