问题描述
所以我一直在尝试将 react 应用程序的一部分转换为 react-native。 作为反应,我有一个用户登录并将所有用户信息保存到 localStorage。这是一段代码:
userActions.js:
dispatch({
type: USER_LOGIN_SUCCESS,payload: data,});
AsyncStorage.setItem("userInfo",JSON.stringify(data));
store.js:
const userInfoFromStorage = localStorage.getItem("userInfo")
? JSON.parse(localStorage.getItem("userInfo"))
: null;
const initialState = {
userLogin: { userInfo: userInfoFromStorage },};
LoginPage.jsx:
// redirect to register
const redirect = location.search ? location.search.split("=")[1] : "/";
const userLogin = useSelector((state) => state.userLogin);
const { loading,error,userInfo } = userLogin;
useEffect(() => {
if (userInfo) {
history.push(redirect);
}
},[history,userInfo,redirect]);
现在的问题是我如何在 react-native 中做到这一点?尝试过这种方式,但似乎不起作用:
userActions.js:
dispatch({
type: USER_LOGIN_SUCCESS,JSON.stringify(data));
store.js:
const userInfoFromStorage = AsyncStorage.getItem('userInfo')
const initialState = {
userLogin: { userInfo: userInfoFromStorage },};
在 react-native 项目中的 store.js 中,我尝试解析它,但它在 react-native 中引发错误,所以我猜 AsyncStorage 会自动执行此操作?
现在通常当我启动应用程序时它会自动登录(不确定用户是否正确,因为我看不到它)。
Login.screen.jsx:
const userLogin = useSelector((state) => state.userLogin);
const { loading,userInfo } = userLogin;
useEffect(() => {
if (userInfo) {
navigation.navigate("Main",{ screen: "Orders" });
}
},[userInfo,navigation]);
const submitHandler = (e) => {
dispatch(login(email,password));
console.log('login')
};
但是,如果我退出,什么也不会发生,我也无法重新登录。除非我重新启动应用程序,否则它会自动重新登录用户。
这也是注销操作:
userActions.js:
export const logout = () => (dispatch) => {
// localStorage.removeItem("userInfo");
// this is where I'm trying to change from localStorage to Async on logout,but it doesn't seem to be doing anything
AsyncStorage.removeItem("userInfo");
dispatch({ type: USER_logoUT });
dispatch({ type: USER_DETAILS_RESET });
// dispatch({ type: ORDER_LIST_MY_RESET });
dispatch({ type: USER_LIST_RESET });
};
感谢任何帮助。谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)