React Native元素类型在useEffect中无效

4dc9hkyq  于 2023-08-07  发布在  React
关注(0)|答案(1)|浏览(131)

下面代码中的useEffect在加载时生成以下错误:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. Check the render method of "Page".

字符串
代码:

import {Stack, useRouter} from "expo-router";
import {Card, Menu, Text} from "@ui-kitten/components";
import {AuthService} from "../services/AuthService";
import {useEffect, useState} from "react";

export default function Page() {
const router = useRouter();

const [auth, setAuth] = useState(null);
const [companies, setCompanies] = useState([]);
const [activeCompany, setActiveCompany] = useState(null);

useEffect(() => {
    AuthService.getAuth().then(tempAuth => {
        setAuth(tempAuth);

        if (tempAuth !== null) {
            setCompanies(tempAuth.companies)

            AuthService.getActiveCompany().then(activeCompany => {
                    console.log(activeCompany, 'active company')
                    setActiveCompany(activeCompany);
                }
            )
        }
    })
}, [])

return (
    <>
        <Stack.Screen
            options={{
                title: activeCompany === null ? 'Choose company' : activeCompany.name,
                headerBackVisible: false
            }}/>
    </>
)
}


将'company'变量记录到控制台确认它实际上不是null或未定义的。我还尝试了硬编码以下内容:

setActiveCompany({id: 'test'});


它给出了同样的错误。
我做错了什么?

xqkwcwgp

xqkwcwgp1#

原来这个问题是由代码中缺少导入引起的,所以如果你遇到这个令人困惑的错误,一定要检查你的导入。

相关问题