导航startTabBasedApp时,React Native Navigation崩溃

时间:2019-03-18 01:47:19

标签: javascript react-native-android react-native-navigation react-native-maps

将React Native Navigation添加到我的应用程序后,它开始崩溃。当我在startTabBasedApp中导航我的应用程序时,它突然崩溃了。我认为这是因为有componentDidMount和React Native Navigation。

这是我的密码。

App.js

 import {Navigation} from 'react-native-navigation';

//Screens
import AuthScreen from './src/screens/Auth/Auth';
import HomeScreen from './src/screens/Home/Home';
import EventCreator from './src/screens/EventCreator/EventCreator';
import EventMap from './src/screens/EventMap/EventMap';

//Register Screens
Navigation.registerComponent("AuthScreen", () => AuthScreen);
Navigation.registerComponent("HomeScreen", () => HomeScreen);
Navigation.registerComponent("EventCreator", () => EventCreator);
Navigation.registerComponent("EventMap", () => EventMap);


//Start an App
Navigation.startSingleScreenApp({
  screen: {
    screen: "AuthScreen",
    title: "Login"
  }
})

Auth.js

这些是AuthScreen(应用启动的地方)的代码

import {View, Text, TouchableOpacity} from 'react-native';
import React, {Component} from 'react';

//Component 
import startMainTabs from '../MainTabs/MainTabs';

class AuthScreen extends Component {
  loginHandler = () => {
    startMainTabs();
  }
  render() {
    return (
      <View>
        <TouchableOpacity onPress={this.loginHandler}>
          <Text>
            Login
          </Text>
        </TouchableOpacity>
        <Text>
          Auth AuthScreen
        </Text>
      </View>
    );
  };
}

然后,当我按下“登录”按钮或TouchableOpacity时,它将导航到startMainTabs,这是3个选项卡。第一个屏幕或选项卡将呈现MapComponent,该应用程序将在其中崩溃,但是如果我第二次再次打开该应用程序,则该应用程序现在将不会崩溃,因为我认为崩溃的原因是用户位置的许可或关于componentDidMount。为了更容易理解,如果我更改第一个屏幕或选项卡首先要渲染的内容,例如Home.js,其中组件只是视图和文本,则它不会崩溃。

EventMap.js

class EventMap extends Component {
constructor(props) {
super(props);

this.state = {
  focusedLocation: {
    latitude: 0,
    longitude: 0,
    latitudeDelta: 0.01,
    longitudeDelta: Dimensions.get('window').width / Dimensions.get('window').height * 0.01
  },
  locationChosen: false,
  markerPosition: {
    latitude: 0,
    longitude: 0
  }
}
}

componentDidMount() {
navigator.geolocation.getCurrentPosition(position => {
  var lat = parseFloat(position.coords.latitude)
  var long = parseFloat(position.coords.longitude)

  var initialRegion = {
    latitude: lat,
    longitude: long,
    latitudeDelta: 0.01,
    longitudeDelta: Dimensions.get('window').width / Dimensions.get('window').height * 0.01
  }

  this.setState({focusedLocation: initialRegion})
  this.setState({locationChosen: true})
  this.setState({markerPosition: initialRegion})
});
 };

  pickLocationHandler = (event) => {
    const coords = event.nativeEvent.coordinate;
    //For animation of map
     this.map.animateToRegion({
      ...this.state.focusedLocation,
      latitude: coords.latitude,
      longitude: coords.longitude
     });
    this.setState(prevState => {
      return {
        focusedLocation: {
          ...prevState.focusedLocation,
      latitude: coords.latitude,
      longitude: coords.longitude
    },
    locationChosen: false
  };
});
};

getLocationHandler = () => {
navigator.geolocation.getCurrentPosition(pos => {
  const coordsEvent = {
    nativeEvent: {
      coordinate:  {
        latitude: pos.coords.latitude,
        longitude: pos.coords.longitude
      }
    }
  };
  this.pickLocationHandler(coordsEvent);
},
err => {
  console.log(err);
  alert("Fetching failed.");
})
};

render () {
let marker = null;
if(this.state.locationChosen) {
  marker = <MapView.Marker coordinate={this.state.markerPosition}/>
}
return (
  <View>
    <TouchableOpacity 
      style={styles.iconContainer}
      onPress={this.getLocationHandler}
    >
      <Icon name="md-locate" size={35}/>
    </TouchableOpacity>
    <Button/>
    <MapView 
      style={styles.map}
      initialRegion={this.state.focusedLocation}
      onPress={this.pickLocationHandler}
      showsUserLocation={true}
      mapType='terrain'
      ref={ref => this.map = ref} //For animating map movement
    >
      {marker}
    </MapView>
  </View>
);
}
}

1 个答案:

答案 0 :(得分:0)

我从未使用过这个库。我使用了“反应导航” 。你可以试试看。希望您不必面对这个问题!