我可以将阵列项目的地图用作其他组件的道具吗?
我是编程的新手,我不知道这里最好的方法是什么。我制作了一个学生卡,并且在这样的组件中有映射:
<ul className="-my-5 divide-y divide-slate-200">
{props.binderMemberships.map((binderMembership) => (
<li key={binderMembership.id} className="py-4">
<div className="flex items-center space-x-4">
<div className="flex-shrink-0">
<Avatar of={binderMembership.binder.student} size="8" />
</div>
</div>
</li>
</ul>
现在我希望化身与此分开,所以我制作了另一个孩子的组成部分,例如:
import Avatar from "./Avatar"
import type { Binder, BinderMember, User } from "@prisma/client"
export interface AvatarLoaderProps {
binderMemberships: (BinderMember & { binder: Binder & { student: User } })[]
user: User
}
export function AvatarLoader(props: AvatarLoaderProps) {
return (
<div className="flex-shrink-0">
<Avatar of={props.binderMemberships.binder.student} size="8" />
</div>
)
}
这是我的问题:我需要通过给我的bindermembership通过在父组件中映射,但我需要在子组件中。我该如何将其作为道具传递,因为它只是一件项目,而只是一个名字?
I'm new to programing and I don't know what's the best approach here. I made a StudentCard and I have the mapping in this component like this:
<ul className="-my-5 divide-y divide-slate-200">
{props.binderMemberships.map((binderMembership) => (
<li key={binderMembership.id} className="py-4">
<div className="flex items-center space-x-4">
<div className="flex-shrink-0">
<Avatar of={binderMembership.binder.student} size="8" />
</div>
</div>
</li>
</ul>
Now I want the Avatar be separate from this, so I made another child component such as:
import Avatar from "./Avatar"
import type { Binder, BinderMember, User } from "@prisma/client"
export interface AvatarLoaderProps {
binderMemberships: (BinderMember & { binder: Binder & { student: User } })[]
user: User
}
export function AvatarLoader(props: AvatarLoaderProps) {
return (
<div className="flex-shrink-0">
<Avatar of={props.binderMemberships.binder.student} size="8" />
</div>
)
}
Here is my issue: I need to pass the binderMembership that is given to me by mapping in the parent component but I need it in the child component. how can I pass it as a props since it's just an item of items and is just a name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您忘了关闭映射标签。
&lt;/li&gt;
之后,它应该具有)}
。其次,您可以将道具传递给嵌套组件或将其放置更高,这很容易做到。您想传递到嵌套,因此必须将其声明在您的组件中。我不知道您是否有类/功能组件,经典功能或箭头。但是它应该像这样:
函数名称函数({ol}){}
我派遣了道具,因为它更容易使用它。
第三是使用想要使用车把
{}
ie{props.id}
的值。仅此而已。
First of all, you forgot to close the tags of mapping. After
</li>
it should have)}
.Second, you can pass props to nested components or put it higher, which is simple to do. You want to pass to nested, so it must declare this in your component. I don't know if you have a class/functional component, a classic function or an arrow. But it should look like this:
function NameOfFunction ({ol}) { }
I dispatched props because it is easier to use it.
Third is to use the value somewhere you want using handlebars
{}
i.e.{props.id}
.And that's all.