[英]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.该图像接受类型
string
或StaticImport
,在您的情况下它似乎是string
,无需担心另一个,但它不接受的是null
或undefined
,所以让我们摆脱那些。
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 不能是
undefined
或null
,但只有 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.