Typescript:变量被分配了一个值但从未使用过,除非它被使用

Mat*_*att 2 typescript reactjs

我收到 Typescript 警告:'isAuthenticated' is assigned a value but never used对于下面的组件 在此输入图像描述

但是,我isAuthenticated在我的组件中使用如下所示。

import { useAuth0 } from '@auth0/auth0-react'

const Profile: React.FC = () => {
    const { user, isAuthenticated, isLoading } = useAuth0()

    if (isLoading) {
        return <div>Loading ...</div>
    }

    return (
        <>
            isAuthenticated && (
            <div>
                <img src={user?.picture} alt={user?.name} />
                <h2>{user?.name}</h2>
                <p>{user?.email}</p>
            </div>
            )
        </>
    )
}

export default Profile
Run Code Online (Sandbox Code Playgroud)

为什么当我使用该变量时会收到此警告?

小智 5

你用Logical && Operator错了。()用。。。来代替{}

更多信息: https: //reactjs.org/docs/conditional-rendering.html