.map不是功能反应,轴问题

发布于 2025-02-08 20:29:32 字数 1449 浏览 1 评论 0原文

使用地图功能,我有一些问题。

在以下代码中,我使用Axios通过稀有的API获取NFT数据(效果很好,我可以将对象作为响应得到承诺),

import Link from 'next/link'
import { useWeb3Context } from '../../context'
import { Web3Button } from '../../components'
import axios from 'axios'
import React, { useState } from 'react'
import NFTContainer from '@/components/NFTContainer'

const renderNotConnectedContainer = () => <Web3Button />

const fetchOwnedTokens = async (owner) => {
  try {
    const result = await axios.get(`https://ethereum-api.rarible.org/v0.1/nft/items/byOwner?owner=${owner}`)

    return [result.data.items]
  } catch (err) {
    console.error(err)
    return []
  }
}

export const ChooseProductView = () => {
  const { address } = useWeb3Context()

  if (!address) {
    return renderNotConnectedContainer()
  } else {
    const data = fetchOwnedTokens(address)
    console.log(data)

    return (
      <div className="flex items-center justify-center">
        <NFTContainer nfts={data} />
      </div>
    )
  }
}

然后我试图使用道具将响应传递给容器文件,但我得到了一个错误 - 无手工运行时错误 TypeError:nfts.map不是功能 我认为这是因为NFT不是数组数据类型。有什么建议吗?被卡住了一段时间

import React from 'react'
import NFTCard from './NFTCard'

const NFTContainer = ({ nfts }) => {
  return (
    <div>
      {nfts.map((nft, index) => (
        <NFTCard nft={nft} key={index} />
      ))}
    </div>
  )
}

export default NFTContainer

I am having a bit of a problem using the map function.

In the below code I am fetching NFT data through a rarible api using Axios (Works perfectly fine, I get a promise with the object as a response)

import Link from 'next/link'
import { useWeb3Context } from '../../context'
import { Web3Button } from '../../components'
import axios from 'axios'
import React, { useState } from 'react'
import NFTContainer from '@/components/NFTContainer'

const renderNotConnectedContainer = () => <Web3Button />

const fetchOwnedTokens = async (owner) => {
  try {
    const result = await axios.get(`https://ethereum-api.rarible.org/v0.1/nft/items/byOwner?owner=${owner}`)

    return [result.data.items]
  } catch (err) {
    console.error(err)
    return []
  }
}

export const ChooseProductView = () => {
  const { address } = useWeb3Context()

  if (!address) {
    return renderNotConnectedContainer()
  } else {
    const data = fetchOwnedTokens(address)
    console.log(data)

    return (
      <div className="flex items-center justify-center">
        <NFTContainer nfts={data} />
      </div>
    )
  }
}

Then I am trying to pass the response to a container file using props, but I get an error --Unhandled Runtime Error
TypeError: nfts.map is not a function
which I think is because NFTs are not of an array datatype. Any suggestions, please? Have been stuck for a while

import React from 'react'
import NFTCard from './NFTCard'

const NFTContainer = ({ nfts }) => {
  return (
    <div>
      {nfts.map((nft, index) => (
        <NFTCard nft={nft} key={index} />
      ))}
    </div>
  )
}

export default NFTContainer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷血 2025-02-15 20:29:32

您无法在对象上使用地图,因此应该将其转换为:

<div>
  {Object.keys(nfts).map((nft, index) => (
    <NFTCard nft={nft} key={index} />
  ))}
</div>

You can not use map on object so you should convert it :

<div>
  {Object.keys(nfts).map((nft, index) => (
    <NFTCard nft={nft} key={index} />
  ))}
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文