简体   繁体   English

具有 auth0 的 NextJS 图像组件

[英]NextJS Image Component with auth0

I am trying to integrate auth0 with nextJS and am having an issue with the next.js Image component.我正在尝试将 auth0 与 nextJS 集成,但 next.js Image组件出现问题。 First, let's take a look at the code:首先,让我们看一下代码:

import Image from "next/image"
import { useUser } from "@auth0/nextjs-auth0"

export default function Profile() {
  const { user, error, isLoading } = useUser()

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

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

This is basically a direct copy from their sample nextjs integration, except that I changed the img tag to the nextjs Image component.这基本上是他们的示例 nextjs 集成的直接副本,只是我将img标签更改为 nextjs Image组件。 Ie, this: <Image src={user.picture} alt={user.name} /> instead of this <img src={user.picture} alt={user.name} /> .即,这个: <Image src={user.picture} alt={user.name} />而不是这个<img src={user.picture} alt={user.name} />

However, this has lead to a typescript error -- as shown in this screenshot:但是,这导致了 typescript 错误——如以下屏幕截图所示:

打字稿错误图像

It seems to me that the nextjs image component is expecting a type of string | StaticImport在我看来,nextjs 图像组件需要一种string | StaticImport string | StaticImport whereas auth0 has setup a type of string | null | undefined string | StaticImport而 auth0 设置了一种string | null | undefined string | null | undefined string | null | undefined - as such the types clash and I am getting an error. string | null | undefined - 因此类型冲突,我收到错误。

Assuming that that is correct, any idea how can I fix that?假设那是正确的,知道我该如何解决吗? Alternatively, if the problem is something else -- any idea what is the problem and how I can fix that?或者,如果问题出在其他地方——知道问题出在哪里以及我该如何解决吗?

Thanks.谢谢。

It seems that the user.picture is optional (at least from typescript's point of view), meaning it might be set or not, so you need to handle what happens if it's not set.似乎user.picture是可选的(至少从打字稿的角度来看),这意味着它可能已设置或未设置,因此您需要处理未设置时发生的情况。 The image accepts type string or StaticImport , and in your case it seems to be string , no need to worry about the other one, but what it doesn't accept is null or undefined , so let's get rid of those.该图像接受类型stringStaticImport ,在您的情况下它似乎是string ,无需担心另一个,但它不接受的是nullundefined ,所以让我们摆脱那些。

I'm assuming from the error message that the user can't be undefined or null , but only the.picture can be, so in that case I would fix it like this:我从错误消息中假设 user 不能是undefinednull ,但只有 the.picture 可以,所以在那种情况下我会像这样修复它:

<Image src={user.picture ?? ''} alt={user.name ?? ''} />

That sets it to empty string if the value is null or undefined.如果值为 null 或未定义,则将其设置为空字符串。 If also the user can be undefined, then the same works with minor change of adding few more ?如果用户也可以是未定义的,那么同样可以通过添加更多的小改动来实现? : :

<Image src={user?.picture ?? ''} alt={user?.name ?? ''} />

Set the width and the height in you Image component.在 Image 组件中设置宽度和高度。

import Image from "next/image"
import { useUser } from "@auth0/nextjs-auth0"

export default function Profile() {
  const { user, error, isLoading } = useUser()

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

  return (
    user && (
      <div>
        <Image 
          src={user.picture} 
          width="100"
          height="100"
          alt={user.name} 
        />
        <h2>{user.name}</h2>
        <p>{user.email}</p>
      </div>
    )
  )
}

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

相关问题 使用 NextJS + Auth0 传递查询参数或正文? - Passing query params or body with NextJS + Auth0? 使用 Auth0 在 Vercel 上部署:无法解析“/vercel/path0/pages”中的“@auth0/nextjs-auth0” - Deployment on Vercel with Auth0: Can't resolve '@auth0/nextjs-auth0' in '/vercel/path0/pages' 如何将 auth0 身份验证添加到 nextjs 13 应用程序 - How to add auth0 authentication to a nextjs 13 application 为什么 auth0/nextjs useUser 挂钩中的用户对象缺少属性? - Why User object in auth0/nextjs useUser hook is missing properties? NextJS 项目:立即将用户发送到 Auth0 页面 - NextJS project: immediately send user to Auth0 page 使用 Auth0 使用 NextJS 应用程序获取 ECONNREFUSED 调用外部 API - Getting ECONNREFUSED calling an external API with NextJS application using Auth0 Auth0 Lock组件未显示在React with container选项中 - Auth0 Lock Component not showing in React with container option 我可以检查 Auth0 用户是否已经在我的自定义 NextJS _app.js 级别登录? - Can I check if a Auth0 user is logged in already at the level of my custom NextJS _app.js? 如何在 nextjs 图像组件中显示占位符图像 - How to show a placeholder image in nextjs image component 如何使用@auth0/auth0-react 在 class 组件中获取 POST - How to fetch POST in a class component using @auth0/auth0-react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM