[英]How to make child values inside map function editable via props
Is there any way to make the mapping child values input editable via props?有什么方法可以通过道具使映射子值输入可编辑? I have this simple accordion in my Next.js App which I'd like to reuse with different inputs and what I am naming
listItemTitle
can vary from input to input.我的 Next.js 应用程序中有这个简单的手风琴,我想用不同的输入重用它,我命名的
listItemTitle
可能因输入而异。 This can either be something like item.question[0].questionTitle
or item.partner[0].partnerLastName
.这可以是
item.question[0].questionTitle
或item.partner[0].partnerLastName
。
How do I make this as scalable as possible?我如何使其尽可能可扩展?
Eg例如
// Child Component
const SimpleAccordion = ({ listItems, listItemTitle }) => {
return (
<section>
{
listItems && listItems.map((item, index) => (
<li key={ index }>
{ listItemTitle } // Make this editable via props
</li>
))
}
</section>
)
}
// Parent Component
...
<SimpleAccordion
listItems={ pageData.questionSelector }
listItemTitle={ question[0].questionTitle }
/>
...
If I understand right you want to make SimpleAccordion generic, in that case you can pass the component to use to that function.如果我理解正确,您想让 SimpleAccordion 通用,在这种情况下,您可以将要使用的组件传递给该函数。
// Parent Component
...
import QuestionListItem from './QuestionListItem'
...
<SimpleAccordion
listItems={ pageData.questionSelector }
listItemComponent={QuestionListItem}/>
...
or
...
import PartnerListItem from './PartnerListItem'
...
<SimpleAccordion
listItems={ pageData.questionSelector}
listItemComponent={PartnerListItem}/>
This will be the generic SimpleAccordion :这将是通用的 SimpleAccordion :
// Generic component
const SimpleAccordion = ({ listItems, listItemComponent }) => {
let Component = listItemComponent
return (
<section>
{
listItems && listItems.map((item, index) => (
<li key={ index }>
<Component item={item}/>
</li>
))
}
</section>
)
}
You can declare as many list items as you want, for example:您可以根据需要声明任意数量的列表项,例如:
const QuestionListItem = ({item}) => {
return (
<span>{item.question[0].questionTitle}</span>
)
}
const PartnerListItem = ({item}) => {
return (
<span>{item.partner[0].partnerLastName}</span>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.