尝试描述组件2的输入,但收到错误。请告知如何正确传递和描述数组输入。
型号:
interface IIncident {
id: string,
title: string
}
组件1:
interface IncidentManagerProps {
}
interface IncidentManagerState {
incidents: IIncident[]
}
class IncidentManager extends Component<IncidentManagerProps, IncidentManagerState> {
constructor(props: IncidentManagerProps) {
super(props);
this.state = {
incidents: []
}
}
render = () => {
const {incidents} = this.state;
return (
<div className={'inc-cont'}>
<p>All Incidents</p>
<div className={'all-inc'}>
//Error displayed here
<Incidents incidents={incidents}/>
</div>
</div>
);
}
}
组件2:任何类型的事件都可以解决问题,但这不是一个好方法
interface Incidents {
incidents: IIncident[]
}
const Incidents = (props: Incidents) => props.incidents.map((inc: IIncident) => (
<Incident key={inc.id} title={inc.title}/>
));
错误消息:
JSX element type 'Element[]' is not a constructor function for JSX elements.
Type 'Element[]' is missing the following properties from type 'Element': type, props, key TS2605
答案 0 :(得分:1)
render()无法返回elements数组,只能返回单个元素。尝试将第二个组件的输出包装到这样的任何元素上:
const Incidents = (props: {incidents: IIncident[]}) => <>
{props.incidents.map(inc =>
<Incident key={inc.id} title={inc.title}/>
)}
</>;