Javascript 反应本机-开关导航器工作不正常
我使用开关导航器、底部选项卡导航器和堆栈导航器,如下所示Javascript 反应本机-开关导航器工作不正常,javascript,react-native,axios,react-navigation,Javascript,React Native,Axios,React Navigation,我使用开关导航器、底部选项卡导航器和堆栈导航器,如下所示 App.js Login.js 只有在尝试使用Switch Navigator时,才会出现此问题。没有开关导航器,也就是说,只使用堆栈导航器就可以正常工作。使用switch navigator的主要目的是维护控制流(当登录用户按下后退按钮或等效按钮时,退出系统主屏幕,而不是导航到登录屏幕) 请说明如何修复此错误 谢谢。在彻底检查之后,我意识到问题并不是直接与switch navigator有关,而是switch navigator和编码错
App.js Login.js 只有在尝试使用Switch Navigator时,才会出现此问题。没有开关导航器,也就是说,只使用堆栈导航器就可以正常工作。使用switch navigator的主要目的是维护控制流(当登录用户按下后退按钮或等效按钮时,退出系统主屏幕,而不是导航到登录屏幕)
请说明如何修复此错误
谢谢。在彻底检查之后,我意识到问题并不是直接与switch navigator有关,而是switch navigator和编码错误的综合结果。 如果仔细查看上面的代码段,您会发现在Login.js的componentDidMount中,我正在检查用户是否已经通过身份验证,方法是检查是否存储了任何令牌。i、 e
if(this.getToken()!==null)
。
经过数小时的调试,我发现this.getToken()
没有返回任何内容,这意味着调用函数将返回未定义的,并且在if块中,我正在检查值是否为null
。因此,基本上,当用户登录并尝试注销时,异步存储值将被清除并导航到登录屏幕。当登录屏幕显示时,componentDidMount将启动,调用this.getToken()将返回undefined
,这不等于null
,这意味着执行if块,该块将再次导航到配置文件屏幕,完成一个循环。
一个简单的修复方法是将条件更改为if(this.getToken()!==undefined&&this.getToken()!==null)或if(this.getToken())
提示:进行健全性检查时,一个好的经验法则是覆盖所有可能的情况(NaN、未定义、null)(仅当函数可以返回所述值时),最好使用if(function()){…}
const SignoutScreen =()=>{}
const bottomTabNavigator = createBottomTabNavigator({
Profile:{
screen:Profile,
navigationOptions:{headerShown:false}
},
Signout: {
screen: SignoutScreen,
navigationOptions: {
tabBarLabel: 'Signout',
tabBarIcon: ({ tintColor }) => (
<SimpleLineIcons name="logout" color={tintColor} size={20} />
),
tabBarOnPress: async ({navigation}) => {
console.log("Pressed")
await AsyncStorage.clear();
navigation.navigate('Login');
}
}
},
Other:{screen:Other,navigationOptions:{tabBarLabel:'Screen C',headerShown:false}}},
{
defaultNavigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
//let IconComponent = Ionicons;
let iconName;
if (routeName === 'Profile') {
iconName = focused
? 'users'
: 'users';
// Sometimes we want to add badges to some icons.
// You can check the implementation below.
//IconComponent = HomeIconWithBadge;
} else if (routeName === 'Other') {
iconName = focused ? 'comments' : 'comments';
}
// You can return any component that you like here!
return <FontAwesome name={iconName} size={25} color={tintColor} />;
},
}),
tabBarOptions: {
activeTintColor: 'tomato',
inactiveTintColor: 'gray',
},
})
const switchNavigation = createSwitchNavigator({
Login:{
screen:Login,
navigationOptions:{
headerShown:false,
},
},
Other:{
screen:bottomTabNavigator,
navigationOptions:{
headerShown:false,
}
},
})
const AppStack = createStackNavigator({
Login:{
screen:switchNavigation,
navigationOptions:{
headerShown:false
}
},
SignUp:{
screen:SignUp,
navigationOptions:{
headerShown:false
}
}
},{
initialRouteName:'Login'
}
)
const container = createAppContainer(AppStack)
export default container
.
.
.
async componentDidMount() {
this._isMounted = true
//this.getToken()
await AsyncStorage.getItem("token").then((token) => {
var tkn = JSON.parse(token)
this.setState({
token: tkn
});
}).catch(err => {
console.log(err)
})
await AsyncStorage.getItem("username").then((usr) => {
var usrnm = JSON.parse(usr)
this.setState({
username:usrnm
});
}).catch(err => {
console.log(err)
})
axios.get('http://127.0.0.1:8000/endpoint/', { headers: { 'Authorization': " Token " + this.state.token } })
.then(response => {
console.log(response)
})
.catch(error => {
console.log("Error is:" + error) //This line is executed whenever I press logout.
//this.props.navigation.navigate('Login')
})
}
async getToken() {
this._isMounted = true
try {
var string = await AsyncStorage.getItem("token")
var tkn = JSON.parse(string)
//alert(tkn)
this.setState({ token: tkn }, () => console.log("Token value set"))
}
catch (error) {
console.log("Something went wrong")
}
}
componentWillUnmount() {
this._isMounted = false
}
.
.
.
.
.
.
componentDidMount(){
this._isMounted = true
if(this.getToken()!==null){ //checking if token exists or not.
this.props.navigation.navigate('App')
}
}
async getToken(){
try{
var string = await AsyncStorage.getItem("token")
var tkn = JSON.parse(string)
//console.log(tkn)
this.setState({token:tkn})
}
catch(error){
console.log("Something went wrong")
}
}
.
.
.