Reactjs 条件组件呈现反应本机

Reactjs 条件组件呈现反应本机,reactjs,react-native,mobile,Reactjs,React Native,Mobile,我正在尝试渲染条件组件。我有MapView和位置,我想实现的是:如果位置是从用户处授予的,或者找不到位置,我想呈现一个文本输入,用户可以手动键入他们的位置,而不是MapView。这是我的密码 export default class GpsLocation extends React.Component { constructor(props) { super(props); this.state = { region: null, }; this

我正在尝试渲染条件组件。我有MapView和位置,我想实现的是:如果位置是从用户处授予的,或者找不到位置,我想呈现一个文本输入,用户可以手动键入他们的位置,而不是MapView。这是我的密码

export default class GpsLocation extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      region: null,
    };
    this._getLocationAsync();
  }

  _getLocationAsync = async () => {
    let { status } = await Permissions.askAsync(Permissions.LOCATION);
    let location = await Location.getCurrentPositionAsync({
      enableHighAccuracy: true,
    });

    let region = {
      latitude: location.coords.latitude,
      longitude: location.coords.longitude,
      latitudeDelta: 0.0045,
      longitudeDelta: 0.0045,
    };
    this.setState({ region: region });
  };

  render() {
    if (this.status === "granted") {
      return (
        <View style={styles.locationContainer}>
        <Text style={styles.note}>Adresa</Text>
        <MapView
          initialRegion={this.state.region}
          showUserLocation={true}
          showCompass={true}
          rotateEnabled={false}
          style={{ flex: 1, borderRadius: 10 }}
        ></MapView>
      </View>
      );
    } else {
      return (
        <TextInput/>
      );
    }
  }
}
导出默认类GpsLocation扩展React.Component{
建造师(道具){
超级(道具);
此.state={
区域:空,
};
这是。_getLocationAsync();
}
_getLocationAsync=async()=>{
让{status}=wait Permissions.askAsync(Permissions.LOCATION);
让位置=等待位置。getCurrentPositionAsync({
EnableHighAccurance:正确,
});
让区域={
纬度:location.coords.latitude,
经度:location.coords.longitude,
纬度德尔塔:0.0045,
纵向德尔塔:0.0045,
};
this.setState({region:region});
};
render(){
如果(this.status==“已授予”){
返回(
肾上腺
);
}否则{
返回(
);
}
}
}
试试这种方法

constructor(props) {
    super(props);
    this.state = {
      region: null,
      status : undefined, // add initially `undefined`
    };
    this._getLocationAsync();
  }

_getLocationAsync = async () => {
    let { status } = await Permissions.askAsync(Permissions.LOCATION);
    .......
    this.setState({ ... , status }); // update status here
  };

render() {
  // change here also
  return this.state.status === "granted" ? (
        <View style={styles.locationContainer}>
          <Text style={styles.note}>Adresa</Text>
          <MapView
            initialRegion={this.state.region}
            showUserLocation={true}
            showCompass={true}
            rotateEnabled={false}
            style={{ flex: 1, borderRadius: 10 }}
          ></MapView>
      </View>
      ) : <TextInput/> ;       
  }
构造函数(道具){
超级(道具);
此.state={
区域:空,
状态:未定义,//添加初始值“未定义”`
};
这是。_getLocationAsync();
}
_getLocationAsync=async()=>{
让{status}=wait Permissions.askAsync(Permissions.LOCATION);
.......
this.setState({…,status});//在此处更新状态
};
render(){
//也在这里换车
返回this.state.status==“已授予”(
肾上腺
) :  ;       
}
试试这种方法

constructor(props) {
    super(props);
    this.state = {
      region: null,
      status : undefined, // add initially `undefined`
    };
    this._getLocationAsync();
  }

_getLocationAsync = async () => {
    let { status } = await Permissions.askAsync(Permissions.LOCATION);
    .......
    this.setState({ ... , status }); // update status here
  };

render() {
  // change here also
  return this.state.status === "granted" ? (
        <View style={styles.locationContainer}>
          <Text style={styles.note}>Adresa</Text>
          <MapView
            initialRegion={this.state.region}
            showUserLocation={true}
            showCompass={true}
            rotateEnabled={false}
            style={{ flex: 1, borderRadius: 10 }}
          ></MapView>
      </View>
      ) : <TextInput/> ;       
  }
构造函数(道具){
超级(道具);
此.state={
区域:空,
状态:未定义,//添加初始值“未定义”`
};
这是。_getLocationAsync();
}
_getLocationAsync=async()=>{
让{status}=wait Permissions.askAsync(Permissions.LOCATION);
.......
this.setState({…,status});//在此处更新状态
};
render(){
//也在这里换车
返回this.state.status==“已授予”(
肾上腺
) :  ;       
}

请解释您面临的问题?请解释您面临的问题?无法识别this.status,并且它未连接到let{status}=wait Permissions.askAsync(Permissions.LOCATION)@ErmiraBKajtazi欢迎:-)也请接受作为答案:-)抱歉,我测试了它,但不起作用。状态==“已授予”应该是其他内容,因为状态无法识别,你能帮我吗?同样的,只是显示文本输入,当im为locationChange
this.status
授予权限时,不在地图视图中,请更改为
this.state.status
,然后重试。我已经更新了alsothis.status未被识别,并且它没有连接到let{status}=wait Permissions.askAsync(Permissions.LOCATION)@ErmiraBKajtazi欢迎:-)也请接受作为答案:-)抱歉,我测试了它,但不起作用。状态==“已授予”应该是其他内容,因为状态无法识别,你能帮我吗?同样的,只是显示文本输入,当im为locationChange
this.status
授予权限时,不在地图视图中,请更改为
this.state.status
,然后重试。我也更新了