我正在做一个反应项目。我有自己的 API 来获取信息。我正在使用 useEffect 挂钩从 API 获取配置文件信息。我的问题是,当页面第一次安装时,我可以毫无问题地获取数据,但如果我刷新页面,它根本不起作用。我知道我必须为 useEffect 提供第二个参数。我尝试将配置文件作为第二个参数,甚至调度了 getCurrentProfile 函数,但是当我这样做时,它会不断地触发获取请求。如果有人能帮助我,我会很高兴。谢谢。
这是我的个人资料组件:
export const Profile = () => {
const dispatch = useDispatch();
useEffect(() => {
dispatch(getCurrentProfile());
}, [])
const profileReducer = useSelector((state) => state.profile);
const authReducer = useSelector((state) => state.auth);
const { profile, error, loading } = profileReducer;
const { user } = authReducer;
console.log("loading", loading)
console.log("profile", profile)
return loading && profile === null ? (
<div >
<Spinner />
</div>
) :
Run Code Online (Sandbox Code Playgroud)
这是我的个人资料操作:
export const getCurrentProfile = () …
Run Code Online (Sandbox Code Playgroud)