Safari 没有更新我的 useState 布尔值
Safari not updating my useState boolean value
我的身份验证上下文通过将值为 true 或 false 的登录状态保存到本地存储来工作
它在所有浏览器上工作正常但最近它停止在 safari 上工作并且状态只显示 null 作为值
可能是什么原因造成的?我使用 react-router-dom 中的 useParams 生成的一些组件页面最终在 safari 上显示 404 错误,但在 chrome
上运行良好
验证上下文代码如下:
import React, { createContext, useContext, useEffect, useState } from "react";
const AuthContext = createContext({});
const AuthProvider = (props) => {
const [loading, setLoading] = useState(true);
const [loggedIn, setLoggedin] = useState(false);
const token = localStorage.getItem("userToken");
useEffect(() => {
const data = localStorage.getItem('logged-in');
setLoggedin(JSON.parse(data));
setLoading(false);
}, [])
useEffect(() => {
localStorage.setItem('logged-in', loggedIn)
},[loggedIn])
const login = () => {
setTimeout(() => {
setLoggedin(true);
}, 500);
};
const logout = () => {
localStorage.clear();
setTimeout(() => {
setLoggedin(false);
}, 1000);
};
const authContextValue = { login, loggedIn, logout, loading };
return <AuthContext.Provider value={authContextValue} {...props} />;
};
const useAuth = () => useContext(AuthContext);
export { AuthProvider, useAuth };
错误来自 safari 没有自动添加“/”到我的端点的末尾,而 google chrome 会。所以我继续手动添加它并且它工作正常
我的身份验证上下文通过将值为 true 或 false 的登录状态保存到本地存储来工作
它在所有浏览器上工作正常但最近它停止在 safari 上工作并且状态只显示 null 作为值
可能是什么原因造成的?我使用 react-router-dom 中的 useParams 生成的一些组件页面最终在 safari 上显示 404 错误,但在 chrome
上运行良好验证上下文代码如下:
import React, { createContext, useContext, useEffect, useState } from "react";
const AuthContext = createContext({});
const AuthProvider = (props) => {
const [loading, setLoading] = useState(true);
const [loggedIn, setLoggedin] = useState(false);
const token = localStorage.getItem("userToken");
useEffect(() => {
const data = localStorage.getItem('logged-in');
setLoggedin(JSON.parse(data));
setLoading(false);
}, [])
useEffect(() => {
localStorage.setItem('logged-in', loggedIn)
},[loggedIn])
const login = () => {
setTimeout(() => {
setLoggedin(true);
}, 500);
};
const logout = () => {
localStorage.clear();
setTimeout(() => {
setLoggedin(false);
}, 1000);
};
const authContextValue = { login, loggedIn, logout, loading };
return <AuthContext.Provider value={authContextValue} {...props} />;
};
const useAuth = () => useContext(AuthContext);
export { AuthProvider, useAuth };
错误来自 safari 没有自动添加“/”到我的端点的末尾,而 google chrome 会。所以我继续手动添加它并且它工作正常