React Native android set set array не работает - PullRequest
1 голос
/ 27 мая 2019

Android Set массив состояний не работает. React-native MapView componentWillMount состояние обновления устанавливает положение карты не работает. Помогите мне, пожалуйста.

Состояния:

constructor(props) {
    super(props);
    this.state = {
        loading: '',
        mapRegion: {
          latitude: 37.78825,
          longitude: -122.4324,
          latitudeDelta: 0.0922,
          longitudeDelta: 0.0421,
        },
        location: {
          coords: {
            latitude: 37.78825,
            longitude: -122.4324,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          }
        }
    };
  }

компонент будет монтироваться

async componentWillMount() {
this.setState({
          mapRegion: {
            latitude: responseJson.latitude,
            longitude: responseJson.longitude,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          },
          location: {
            coords: {
              latitude: responseJson.latitude,
              longitude: responseJson.longitude,
              latitudeDelta: 0.0922,
              longitudeDelta: 0.0421,
            }
          },
 });
}

Возвратный вид без изменения положения

<MapView style={{ width: '100%', height: '100%' }}
region={{
     latitude: this.state.location.coords.latitude,
     longitude: this.state.location.coords.longitude,
     latitudeDelta: 3,
      longitudeDelta: 3
 }}
zoomEnabled = {true}>
<MapView.Marker coordinate={this.state.location.coords} title={strings('Location.Buradasiniz')} />
<MapView.Circle center={this.state.location.coords} radius={this.state.radius} strokeColor='red' fillColor="rgba(255, 0, 0, 0.20)"/></MapView>

1 Ответ

0 голосов
/ 27 мая 2019

Попробуйте это:

async componentWillMount() {
await this.setState({
          mapRegion: {
            latitude: responseJson.latitude,
            longitude: responseJson.longitude,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          },
          location: {
            coords: {
              latitude: responseJson.latitude,
              longitude: responseJson.longitude,
              latitudeDelta: 0.0922,
              longitudeDelta: 0.0421,
            }
          },
 });
}

Когда Вы используете async, после этого Вы должны написать куда-нибудь await, вот документы: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Или попробуйте без async await вот так:

componentWillMount() {
 this.setState({
          mapRegion: {
            latitude: responseJson.latitude,
            longitude: responseJson.longitude,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          },
          location: {
            coords: {
              latitude: responseJson.latitude,
              longitude: responseJson.longitude,
              latitudeDelta: 0.0922,
              longitudeDelta: 0.0421,
            }
          },
 });
}

Удачи))

...