使用效果仅在刷新页面后起作用
我在上下文中使用使用效果。我认为使用效果应在加载页面后运行,但是它不起作用,但是如果我刷新页面(F5),则可以使用。
我是新的使用React,但我认为它应该与Arg一样的空数组使用。
这是上下文:
import { createContext, useState, useEffect } from "react";
const PatientContext = createContext();
export const PatientsProvider = (props) => {
const {children} = props;
const [patients, setPatients] = useState([]);
//get the patients from the database
useEffect( () => {
const url = `${import.meta.env.VITE_BACKEND_URL}/api/patients`;
const token = localStorage.getItem("token");
if(!token) return;
const getPatients = async () => {
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
"Authorization" : `Bearer ${token}`
}
});
const result = await response.json();
if (!response.ok){
throw new Error(result.msg);
}
const {createdAt, __v, updatedAt, ...savedPatient} = result;
setPatients(result)
setLoading(false);
} catch (error) {
setLoading(false)
setAlert({msg: error.message, error1: true})
console.log(error)
}
}
getPatients();
}, [])
编辑:
现在我意识到问题是在登录页面中第一次使用效果(在我需要之前,因为在登录页面后需要它)因为PatientProvider是所有组件的父母,甚至是登录组件,但是现在我不知道如何将此提供商设置为“登录组件”。
这是我的路线:
function App() {
return (
<BrowserRouter>
<AuthProvider>
<PatientsProvider>
<Routes>
<Route path="/" element={< Authlayout />}>
<Route index element={ <Login /> } />
<Route path="sign-up" element= {<SignUp />}/>
<Route path="forgot-password" element= {<ForgotPassword />}/>
<Route path="forgot-password/:token" element= {<ResetPassword />}/>
<Route path="confirm-account/:token" element= {<Confirm />}/>
</Route>
<Route path="/admin" element={<LoginLayout />}>
<Route index element={<AdminPatients />} />
<Route path="profile" element= {<Profile />}/>
<Route path="change-password" element= {<ChangePass />}/>
</Route>
</Routes>
</PatientsProvider>
</AuthProvider>
</BrowserRouter>
)
}
I am using an useEffect inside a Context. I think the useEffect should run after load the page, but it does not work, but if I refresh the page(F5) then it works.
I am new Using React, but I think it should work with the empty array as arg.
This is the Context:
import { createContext, useState, useEffect } from "react";
const PatientContext = createContext();
export const PatientsProvider = (props) => {
const {children} = props;
const [patients, setPatients] = useState([]);
//get the patients from the database
useEffect( () => {
const url = `${import.meta.env.VITE_BACKEND_URL}/api/patients`;
const token = localStorage.getItem("token");
if(!token) return;
const getPatients = async () => {
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
"Authorization" : `Bearer ${token}`
}
});
const result = await response.json();
if (!response.ok){
throw new Error(result.msg);
}
const {createdAt, __v, updatedAt, ...savedPatient} = result;
setPatients(result)
setLoading(false);
} catch (error) {
setLoading(false)
setAlert({msg: error.message, error1: true})
console.log(error)
}
}
getPatients();
}, [])
EDIT:
Now I realized that the problem is the useEffect render the first time in the login page (before I need it, because I need it after the login page), this is happening because PatientProvider is parent of all the components even Login component, but now I do not know how to set this provider to only "after login components".
Here is my Routes:
function App() {
return (
<BrowserRouter>
<AuthProvider>
<PatientsProvider>
<Routes>
<Route path="/" element={< Authlayout />}>
<Route index element={ <Login /> } />
<Route path="sign-up" element= {<SignUp />}/>
<Route path="forgot-password" element= {<ForgotPassword />}/>
<Route path="forgot-password/:token" element= {<ResetPassword />}/>
<Route path="confirm-account/:token" element= {<Confirm />}/>
</Route>
<Route path="/admin" element={<LoginLayout />}>
<Route index element={<AdminPatients />} />
<Route path="profile" element= {<Profile />}/>
<Route path="change-password" element= {<ChangePass />}/>
</Route>
</Routes>
</PatientsProvider>
</AuthProvider>
</BrowserRouter>
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您的令牌不是在组件安装时设置并调用此usefeft()时设置的。
我可以想到2个选项:
令牌
添加为useeffect()中的依赖项:It seems like your token is not set by the time the component mounts and calls this useEffect().
I can think of 2 options:
token
as a dependency in useEffect():