在JSX中显示对象数据
我的状态有这个对象: console.log(state)
我创建了一个组件来渲染此对象中每个条目的卡片(在对象下)
我已经尝试了所有事情,但是我总是会遇到错误。这是我的最后一次尝试:
import { useState } from "react"
export default function Categories(props: any) {
const [listOfCategories, setListOfCategories] = useState(props.datas.categories)
console.log(listOfCategories)
return (
<>
{listOfCategories.map(category => {
return (
<div key={category} className="relative flex flex-wrap h-96 my-20">
<div className="absolute right-0 top-0 h-full">
<img className="rounded-lg max-w-full h-96" src="https://double-you-design.fr/wp-content/uploads/2016/04/dummy-post-horisontal.jpg" alt="" />
</div>
<div className="absolute -bottom-10 bg-white rounded-lg shadow-lg w-7/12 h-48 p-8">
<p key={category}>{category}</p>
</div>
</div>
)
})
}
</>
)
}
错误是:listOfCategories.map不是函数。
我的问题是:最简单的方法是什么? 您有建议还是例子?
我正在使用打字稿。
非常感谢 !
I have this Object in my state :
console.log(state)
I created a component to render a card for each entry in this object (which has under object)
I have tried everything, but I always get an error. Here is my last attempt :
import { useState } from "react"
export default function Categories(props: any) {
const [listOfCategories, setListOfCategories] = useState(props.datas.categories)
console.log(listOfCategories)
return (
<>
{listOfCategories.map(category => {
return (
<div key={category} className="relative flex flex-wrap h-96 my-20">
<div className="absolute right-0 top-0 h-full">
<img className="rounded-lg max-w-full h-96" src="https://double-you-design.fr/wp-content/uploads/2016/04/dummy-post-horisontal.jpg" alt="" />
</div>
<div className="absolute -bottom-10 bg-white rounded-lg shadow-lg w-7/12 h-48 p-8">
<p key={category}>{category}</p>
</div>
</div>
)
})
}
</>
)
}
the error is : listOfCategories.map is not a function.
My question is : What is the simplest method to do this ?
Do you have a recommandation, or an example ?
I am working with TypeScript.
Thank you very much !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在对象上使用
.map
,因为它是数组的一种方法。您可以使用 so:
这是一个简单的可运行示例,要说明:
另一个选项是更改数据结构,以便您拥有一系列对象而不是对象的对象 - 如果您对该数据结构有控制权。如果没有,您可以首先将对象转换为数组。
代码中的以上方法:
You can't use
.map
on an object because it is a method of Array.You could do it with Object.keys like so:
Here is a simple runnable example to illustrate:
Another option would be to change your data structure so that you have an array of objects instead of an object of objects - if you have control over that data structure. If not, you could first convert the object to an array.
Above method in your code: