.map不是功能反应,轴问题
使用地图功能,我有一些问题。
在以下代码中,我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法在对象上使用地图,因此应该将其转换为:
You can not use map on object so you should convert it :