如何检查native native中的变量是null?

时间:2017-06-05 09:30:08

标签: null compare react-native-android

我正在尝试一个例子。这是我的代码

export default class newApp extends Component {

  render() {
    var data = this.getWeatherFromApiAsync()

    return (
      if(data != null )
      {
        <View>
      <Text>got data</Text>
      </View>
      }else
      {
        <View>
      <Text>no data</Text>
      </View>
      }


    );
  }

   getWeatherFromApiAsync() {
      return fetch('http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=14&APPID=18dcba27e5bca83fe4ec6b8fbeed7827')
        .then((response) => response.json())
        .then((responseJson) => {
          this.setState({isLoading: false, jsonData: responseJson});
          console.log(responseJson);
          return responseJson;
        })
        .catch((error) => {
          console.error(error);
        });
    }
}

这会产生以下错误

E:/REACT_NATIVE_PROJECTS/newApp/index.android.js: Unexpected token (25:6)
  at E:\REACT_NATIVE_PROJECTS\newApp\index.android.js:25:6
processBundleResult
    DevServerHelper.java:381
access$400
    DevServerHelper.java:65
execute
    DevServerHelper.java:333
emitChunk
    MultipartStreamReader.java:69
readAllParts
    MultipartStreamReader.java:116
onResponse
    DevServerHelper.java:321
execute
    RealCall.java:126
run
    NamedRunnable.java:32
runWorker
    ThreadPoolExecutor.java:1113
run
    ThreadPoolExecutor.java:588
run
    Thread.java:818

我做错了什么。是比较部分?

1 个答案:

答案 0 :(得分:2)

这就是我们在反应原生中使用条件渲染的方式:

{1 + 2 == 3&amp;&amp;我只在左手是真的时出现}

更正你的语法。

https://atticuswhite.com/blog/render-if-conditionally-render-react-components/