在我添加react Antive导航到我的应用程序后,它开始崩溃。当我在starttabbasedapp中导航我的应用程序时,它突然崩溃了。我认为这是因为componentdidmount和反应本机导航。
这是我的代码。
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>
);
};
}
然后,当我按登录按钮或触摸可触摸时,它将导航到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>
);
}
}
我从未使用过此库。我使用'React-Navigation'。您可以尝试。希望您不必面对这个问题!