reactjs - 如何在我的动态组件中传递道具
问题描述
我正在做这个完全动态的渲染,我有一个问题,在标题和副标题道具中,我想传递一个在地图中的值,我不想通过硬编码,但我不知道如何我可以通过它。
interface Props {
questions:any[]
}
interface OptionsSurveys {
[key: string]: JSX.Element;
}
const FindSurveyComponents: React.FC<Props> = props => {
const { questions } = props
const components: OptionsSurveys = {
'star-rating': <StarRating
title={'Lorem Ipsum is simply dummy text of the printing and typesetting industry.?'}
subtitle={'Lorem Ipsum has been the industry's standard dummy text ever since the 1500s'}
/>,
'slider-grade': <SliderGrade
title={'Lorem Ipsum is simply dummy text of the printing and typesetting industry.?'}
subtitle={'Lorem Ipsum has been the industry's standard dummy text ever since the 1500s'}
/>,
'default': <Textarea
title={'Lorem Ipsum is simply dummy text of the printing and typesetting industry.?'}
/>
}
const mappingComponents = () => {
return (
questions.map((question:any) => {
const SurveyComponent = components[question.style]
return SurveyComponent
})
)
}
return (
<Fragment>
{questions && mappingComponents()}
</Fragment>
)
}
更新:
我设法像这样更改它,现在我可以将道具发送到每个组件,我只怀疑放置它的类型,放任何
const components: any = {
'star-rating': StarRating,
'slider-grade': SliderGrade,
'default': Textarea
}
const mappingComponents = () => {
return (
questions.map((question:any, index:number) => {
const SurveyComponent = components[question.style]
return <SurveyComponent key={index} />
})
)
}
解决方案
推荐阅读
- java - 设计两种可能的方式
- java - Hibernate,进行本地 sql 查询并获取 get() 上的内容的列名
- ios - 你知道如何在 Swift 中使用 Core Animation 来制作这个动画吗?
- r - 在 R 中将 SQL 求和和计算转换为 dplyr
- slack - 如何创建带有文件附件的 Slack 命令
- reactjs - 为什么 Google AdSense 没有与 Reactjs 一起显示?
- android - Glide:在内存缓存中预加载图像(有或没有磁盘缓存)
- javascript - 对数组值进行分组并添加属性
- c# - 窗口大小与我在设计器中的大小不符
- laravel - Laravel 请求/验证总是返回字符串类型值