import React, { Component } from 'react'
import { Container, Content, Form, Item, Input, Label, Button,Text, Icon} from 'native-base'
import AsyncStorage from '@react-native-community/async-storage';
import authStore from '../store/authStore';
export default class Login extends Component {
constructor(props){
super(props);
this.state={
email:'',
password:''
}
}
handleLogin = async () =>{
let requestObject = {
email: this.state.email,
password: this.state.password
}
authStore.userLogin(requestObject, response => {
this.storeUserData(response.data.data);
this.props.navigation.navigate('Home');
})
}
storeUserData = async (value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem('@userData', jsonValue)
} catch (e) {
console.log(e);
}
}
render() {
return (
<Container>
<Content contentContainerStyle={{flex: 1, justifyContent:'center'}}>
<Form style={{margin:10}}>
<Item rounded last style={{margin:10}}>
<Icon active type="FontAwesome" name='user' />
<Input placeholder='User Name'
onChangeText={(email)=>this.setState({email})}
value={this.state.email}/>
</Item>
<Item rounded last style={{margin:10}}>
<Icon active type="FontAwesome" name='key' />
<Input placeholder='Password'
secureTextEntry
onChangeText={(password)=>this.setState({password})}
value={this.state.password}/>
</Item>
<Button rounded block style={{margin:10}} onPress={() => this.handleLogin()}>
<Text>Sign-In</Text>
</Button>
</Form>
</Content>
</Container>
)
}
}
const AuthStack = createStackNavigator();
AuthStackScreen = () =>
<AuthStack.Navigator>
<AuthStack.Screen name="Login" component={Login} />
</AuthStack.Navigator>
HomeStackScreen = () =>
<HomeStackDrawer.Navigator>
<HomeStackDrawer.Screen name="Home" component={HomeScreen}/>
<HomeStackDrawer.Screen name="Form" component={FormScreen}/>
<HomeStackDrawer.Screen name="Logout" component={Logout}/>
</HomeStackDrawer.Navigator>
export default class App extends Component{
constructor(props){
super(props);
this.state={
isloggedIn:false
}
this.loginStatusCheck();
}
loginStatusCheck = async () =>{
const userToken = await AsyncStorage.getItem('@accessToken');
if (userToken) {
this.setState({isloggedIn:true})
} else {
this.setState({isloggedIn:false})
}
}
render(){
return(
<NavigationContainer>
{this.state.isloggedIn ? <HomeStackScreen/> : <AuthStackScreen/>}
</NavigationContainer>
)
}
}
这是我的App.js,我正在检查用户是否登录,然后相应地加载导航堆栈。我知道问题所在,如果我要注销,我想导航到登录组件,但是this.props.navigation.navigate('Login')
给出了错误。因为我没有返回Login
路线。如何解决这个问题?另外,当我Log in
遇到同一问题时,堆栈中不存在Login
。
预先谢谢你
包含登录组件
答案 0 :(得分:1)
您必须进行一些更改才能解决此问题。您的问题是您试图访问导航堆栈中不存在的屏幕。 最大的问题是在App.js中使用状态变量来处理导航堆栈的切换。您可以通过在应用程序的上下文中维护登录状态来解决此问题。您也可以从其他屏幕进行更新。更新登录状态后,您不必担心导航,而App.js中的条件将为您管理。
代码应类似于以下内容。我已经给出了一个示例登录组件,它将更新上下文。您将不得不切换到功能组件。从您的代码中,我看不到这样做的任何问题。
const AppContext = createContext({
isloggedIn: {},
setLoggedIn: () => {},
});
const Login = () => {
const { setLoggedIn } = useContext(AppContext);
return (
<View>
<Button onPress={() => setLoggedIn(true)} />
</View>
);
};
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
isloggedIn: false,
loading: true,
};
this.loginStatusCheck();
}
setLoggedIn = value => {
this.setState({ isloggedIn: value });
};
loginStatusCheck = async () => {
const userToken = await AsyncStorage.getItem('@accessToken');
if (userToken) {
this.setState({ isloggedIn: true, loading: false });
} else {
this.setState({ isloggedIn: false, loading: false });
}
};
render() {
if (this.state.loading) return <ActivityIndicator />;
return (
<AppContext.Provider
value={{
isloggedIn: this.state.isloggedIn,
setLoggedIn: this.setLoggedIn,
}}>
<NavigationContainer>
{this.state.isloggedIn ? <HomeStackScreen /> : <AuthStackScreen />}
</NavigationContainer>
</AppContext.Provider>
);
}
}
希望这会有所帮助。