react-native:如何做条件网络请求

时间:2019-01-07 05:49:51

标签: javascript reactjs rest api react-native

我正在将random user api用于我的本地反应项目。这就是我想做的:

检查randomUserapi是否具有我要查找的用户名,如果是,请使用get request显示用户的电子邮件

这是我的代码:

import React from 'react';
import {Text, StyleSheet, View, } from 'react-native';
import axios from 'axios'


export default class TestScreen extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      userName1: 'ronja',
      userName2: 'mathys',
    };
  }

  componentWillMount() {
    axios.get('https://randomuser.me/api?results=10')
          .then(response => console.log(response.data.results))
  }

  render() {
    return (
    <View style={{flex:1,justifyContent: 'center', alignItems: 'center'}}>

      <View style={styles.box}>
        <Text>userName1: {this.state.userName1} </Text>

// here I want check if random user api has name 'ronja' if yes, find the email and display

        <Text>email: </Text>
      </View>

      <View style={styles.box}>
        <Text>userName2: {this.state.userName2} </Text>
        <Text>email: </Text>
      </View>

    </View>
    );
  }
}

enter image description here

0 个答案:

没有答案