javascript - 如何动态设置接口以接收共享组件中的道具?
问题描述
我有三个不同的接口,它们作为 props 类型被三个不同的组件接收,事实是,这三个组件是相同的,我只想使用一个组件,但是动态地,它们的值基于接收到的 props。
const UsersOverview: React.FC<{users: Interface[]}> = props => {...}
在 React with JS 中,我只需设置一个 props 并接收值为props.anything
,但使用 typescript 我想知道如何动态设置接口:
我有类似的东西:
接口一:
export default interface Interface1{
id: number,
name: string,
companyId: number
}
接口二:
export default interface Interface2 {
id: number,
email: string,
name: string,
unitId: number,
companyId: number
}
接口3:
export default interface Interface3 {
id: number | null,
name: string | null
}
我的子组件是这样的(我正在使用Interface3
,但我想动态设置它):
import React from 'react'
import Interface1 from '/models/Interface3'
const CompanyOverview: React.FC<{company: Interface3}> = props => {
let [companyName, setCompanyName] = React.useState<string | null>('')
React.useEffect(() => {
setCompanyName(props.company.name)
}, [props.company])
return (
<div>
<p>Contratante: {companyName}</p>
</div>
)
}
export default CompanyOverview
解决方案
推荐阅读
- excel - Data Validation Character Limit Exceeded with Substitute Function
- python - 优化石头剪刀布(新手)
- c++ - SDI12 库不显示带有 + 或 - 登录的测量值
- javascript - 选定单选按钮摘要
- python - 根据每个变量中的值计数对 top5 记录进行排序
- r - 匹配和替换字符向量中的单词
- html - 在浏览器窗口居中的 div 中文本的绝对位置
- delphi - 如何正确使用 TBitmap 对象来保存具有透明度的文件?
- javascript - 在 React 中为键值添加样式
- ios - 当我对表情符号进行子串时,它会崩溃。斯威夫特 iOS