Javascript 无法读取属性';导航';甚至是未定义的';他在工作
我有这个登录屏幕,它的工作,但它显示这个警告,我担心它。当我点击headerRight按钮时,我必须注销并返回登录屏幕 我已经尝试了这个网站提供的一切 这是我的课Javascript 无法读取属性';导航';甚至是未定义的';他在工作,javascript,reactjs,Javascript,Reactjs,我有这个登录屏幕,它的工作,但它显示这个警告,我担心它。当我点击headerRight按钮时,我必须注销并返回登录屏幕 我已经尝试了这个网站提供的一切 这是我的课 class AuthLoadingScreen extends Component{ constructor(props){ super(); this._loadData(); } _loadData = async() => { const is
class AuthLoadingScreen extends Component{
constructor(props){
super();
this._loadData();
}
_loadData = async() => {
const isLoggedIn = await AsyncStorage.getItem('access_token');
console.log(isLoggedIn);
this.props.navigation.navigate(isLoggedIn == null? 'Login' : 'Home');
}
_removeToken = async(navigation) => {
console.log('test');
await AsyncStorage.clear();
navigation.navigate('Login');
}
render(){
return(
<View><ActivityIndicator/></View>
);
}
}
类AuthLoadingScreen扩展组件{
建造师(道具){
超级();
这是。_loadData();
}
_loadData=async()=>{
const isLoggedIn=await AsyncStorage.getItem('access_token');
console.log(isLoggedIn);
this.props.navigation.navigate(isLoggedIn==null?'Login':'Home');
}
_removeToken=异步(导航)=>{
console.log('test');
等待异步存储。清除();
导航。导航('Login');
}
render(){
返回(
);
}
}
这就是我调用_removeToken函数的地方
const StackNavigator = createStackNavigator({
AuthLoading: AuthLoadingScreen,
Login: {
screen: Login,
},
Register: {
screen: Register,
},
Home: {
screen: TabNavigator,
navigationOptions: ({navigation}) => ({
title: 'Insta Clone',
headerLeft: null,
headerTitleStyle: {
textAlign:"center",
flex:1
},
headerRight: (
<Button
icon={
<Icon
name='sign-out'
type='octicon'
color='black'
/>
}
onPress={() => (new AuthLoadingScreen)._removeToken(navigation)} // <--- HERE
buttonStyle={ {backgroundColor: 'transparent'} }
/>
),
headerLeft: (
<Button
icon={
<Icon
name='info'
type='octicon'
color='black'
/>
}
onPress={() => alert("Created by Matheus Melo")}
buttonStyle={ {backgroundColor: 'transparent'} }
/>
),
})
},
});
const StackNavigator=createStackNavigator({
AuthLoading:AuthLoadingScreen,
登录:{
屏幕:登录,
},
登记册:{
屏幕:寄存器,
},
主页:{
屏幕:TabNavigator,
导航选项:({navigation})=>({
标题:“Insta Clone”,
headerLeft:null,
标题样式:{
textAlign:“居中”,
弹性:1
},
头灯:(
(新的AuthLoadingScreen)。_removeToken(导航)}//
),
左校长:(
警报(“由Matheus Melo创建”)}
buttonStyle={{backgroundColor:'transparent'}}
/>
),
})
},
});
您可以直接导航到登录
,无需使用AuthLoadingScreen
参考,以下是更新代码的外观:
const removeToken = async(navigation) => {
console.log('test');
await AsyncStorage.clear();
navigation.navigate('Login');
}
const StackNavigator = createStackNavigator({
AuthLoading: AuthLoadingScreen,
Login: {
screen: Login,
},
Register: {
screen: Register,
},
Home: {
screen: TabNavigator,
navigationOptions: ({navigation}) => ({
title: 'Insta Clone',
headerLeft: null,
headerTitleStyle: {
textAlign:"center",
flex:1
},
headerRight: (
<Button
icon={
<Icon
name='sign-out'
type='octicon'
color='black'
/>
}
onPress={() => removeToken(navigation)}
buttonStyle={ {backgroundColor: 'transparent'} }
/>
),
headerLeft: (
<Button
icon={
<Icon
name='info'
type='octicon'
color='black'
/>
}
onPress={() => alert("Created by Matheus Melo")}
buttonStyle={ {backgroundColor: 'transparent'} }
/>
),
})
},
});
const removeToken=async(导航)=>{
console.log('test');
等待异步存储。清除();
导航。导航('Login');
}
const StackNavigator=createStackNavigator({
AuthLoading:AuthLoadingScreen,
登录:{
屏幕:登录,
},
登记册:{
屏幕:寄存器,
},
主页:{
屏幕:TabNavigator,
导航选项:({navigation})=>({
标题:“Insta Clone”,
headerLeft:null,
标题样式:{
textAlign:“居中”,
弹性:1
},
头灯:(
移除(导航)}
buttonStyle={{backgroundColor:'transparent'}}
/>
),
左校长:(
警报(“由Matheus Melo创建”)}
buttonStyle={{backgroundColor:'transparent'}}
/>
),
})
},
});
在何处声明属性this.props
?@Ele我声明它,但在调用loadData functionNo时它没有显示此警告。构造函数上只有props
作为参数。您从未分配过。可能缺少this.props=props构造函数中的code>,就在this的前面。_loadData()代码>。不起作用://我只是将removeToken作为常量放在类外,它就起作用了!多谢各位MUCH@MatheusMelo很高兴这有帮助。快乐编码