首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何基于FaunaDB数据在Next.js中渲染元素

如何基于FaunaDB数据在Next.js中渲染元素
EN

Stack Overflow用户
提问于 2022-03-04 15:44:58
回答 1查看 43关注 0票数 0

我试图根据用户的职务配置文件状态呈现不同的元素。我使用FaunaDB和useSWR来获取数据,并在执行console.log(UserData)时获取以下对象:

代码语言:javascript
复制
jobProfile: {status: 'active'}

我在页面上导入和调用一个名为profileStatusIndicator(userData)的函数,userData作为参数。

代码语言:javascript
复制
export const profileStatusIndicator = (userData) => {
  console.log(userData) //returns object: jobProfile: {status: 'active'}  
  const profileStatus = (status) =>
    userData?.jobProfile?.status?.includes(status)
  console.log(profileStatus) //returns nothing 

  if (profileStatus("active")) return active
  if (profileStatus("occupied")) return occupied
  if (profileStatus("inactive")) return inactive

  const active = (
    <>
      <span className="absolute top-1 right-1 block h-6 w-6 rounded-full bg-green-400 ring-2 ring-white" />
    </>
  )

  const occupied = (
    <>
      <span className="absolute top-1 right-1 block h-6 w-6 rounded-full bg-yellow-400 ring-2 ring-white" />
    </>
  )

  const inactive = (
    <>
      <span className="absolute top-1 right-1 block h-6 w-6 rounded-full bg-gray-400 ring-2 ring-white" />
    </>
  )
}

执行console.log(userData)返回对象。如果我执行console.log(profileStatus),它不会在控制台中返回任何内容。

你能发现我做错了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-06 16:57:26

我的最初解决方案是在profileStatusIndicator函数中实现一个开关语句:

代码语言:javascript
复制
 const profileStatus = userData?.jobProfile?.status

  const profileStatusIndicator = (profileStatus) => {
    switch (profileStatus) {
      case "active":
        return (
          <span className="absolute top-1 right-1 block h-6 w-6 rounded-full bg-green-400 ring-2 ring-white" />
        )
      case "inactive":
        return (
          <span className="absolute top-1 right-1 block h-6 w-6 rounded-full bg-gray-400 ring-2 ring-white" />
        )

      case "occupied":
        return (
          <span className="absolute top-1 right-1 block h-6 w-6 rounded-full bg-yellow-400 ring-2 ring-white" />
        )

      default:
        return (
          <span className="absolute top-1 right-1 block h-6 w-6 rounded-full bg-gray-400 ring-2 ring-white" />
        )
    }
  }

然后,我调用了我希望它显示的函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71353801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档