简体   繁体   English

我如何在 React 中的条件语句之后使用钩子?

[英]How can I use a hook after conditional statements in React?

I have this following code using the react firebase hooks library :我有以下代码使用反应 firebase 挂钩库

    // load user state 
    const [user, loadingAuthState, errorAuthState] = useAuthState(auth);

    // user is loading...
    if(loadingAuthState)
    {
      return <div>טוען...</div>
    }
    // an error has occured
    else if(errorAuthState)
    {
      alert(errorAuthState);
      return <Redirect to="login"/>
    }
    // if the user is not set, return to the login page
    else if(!(user))
    {
      return (<Redirect to="/login"/>)
    }

    // load data from the user using the user's ID.
    const [value, loading, error] = useDocumentData(doc(db, "users", user.uid));

I get this error:我收到此错误:

Line 46:37: React Hook "useDocumentData" is called conditionally.第 46:37 行:有条件地调用 React Hook“useDocumentData”。 React Hooks must be called in the exact same order in every component render. React Hooks 必须在每个组件渲染中以完全相同的顺序调用。 Did you accidentally call a React Hook after an early return?你是否在提前返回后不小心调用了 React Hook? react-hooks/rules-of-hooks反应钩子/钩子规则

I understand the idea of calling hooks at the top level, however, I need to know the user's ID before using the 2nd hook to read data, so how can I structure my code in a way that doesn't conflict with React's guidelines?我理解在顶层调用钩子的想法,但是,在使用第二个钩子读取数据之前需要知道用户的 ID,那么我如何以不与 React 指南冲突的方式构建我的代码? I'd like to keep the "loading" message that is returning while the user is loading.我想保留在用户加载时返回的“正在加载”消息。

One way of doing it is to split this code into two components so that you return another component when you know the user's ID.一种方法是将此代码拆分为两个部分,以便在知道用户 ID 时返回另一个部分。 Inside of this second component you can have useDocumentData hook unconditionally because you would already know the user's ID passed in through props.在第二个组件中,您可以无条件地使用 useDocumentData 挂钩,因为您已经知道通过 props 传入的用户 ID。

    // load user state 
    const [user, loadingAuthState, errorAuthState] = useAuthState(auth);

    // user is loading...
    if(loadingAuthState)
    {
      return <div>טוען...</div>
    }
    // an error has occured
    else if(errorAuthState)
    {
      alert(errorAuthState);
      return <Redirect to="login"/>
    }
    // if the user is not set, return to the login page
    else if(!(user))
    {
      return (<Redirect to="/login"/>)
    }

    // load data from the user using the user's ID.
    return <MyComponentWithUserId userId={user.uid} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 firebase v9 中使用 useCollection react-firebase 钩子 - How to use useCollection react-firebase hook with firebase v9 如何在特定的 NextJS 中使用 React Hook? (火力地堡身份验证) - How to use react hook in specific NextJS? (Firebase Authentication) 哪个反应挂钩与 firestore onsnapshot 一起使用? - Which react hook to use with firestore onsnapshot? 在 React Native 中删除后如何重新加载页面 - how can i reload the page after deleting in react native 我如何在 useEffect 挂钩中从 firestore 数据库获取 map 数据 - How can I map data from a firestore database in a useEffect hook 使用 React 和 Firestore 登录后如何通过 UID 获取用户详细信息? - How can I get user details by UID after login with React and Firestore? 如何在 arm 模板中使用条件或? - How to use conditional OR in arm template? 如何在 BigQuery 中编写条件 string_agg 函数? - How can I write a conditional string_agg function in BigQuery? 在 React Native 中从其他文件更改值后如何导出变量? - How can I export a variable after value changed from other file in react native? 我应该如何将 firestore object 转换成我可以在 expo/react-native 中使用的东西? - How should I convert a firestore object into something I can use in expo/react-native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM