最近我开始学习ES5的反应,但现在试图在我的应用程序中使用类型 脚本。任何人都可以告诉我为什么我无法使用{this.props.people}打印 值,但它在按预期工作,当我 使用{this.state.people}。我已经从这个网站的例子。 请问我这里缺少什么?this.props.xxx是未定义的,但使用与打字稿的反应,但在ES5中正常工作?
import * as React from 'react';
class About extends React.Component<any, any> {
constructor(props: any) {
super(props);
console.log(About);
const people = [];
for (let i = 0; i < 10; i++) {
people.push({
name: i,
country: i + i
});
}
this.state = {people};
}
public render() {
return (
<div>
{this.props.people.map((person: any, index: number) => (
<p key={index}>Hello, {person.name} from {person.country}!</p>
))}
</div>
);
}
}
export default About;
@Nandu ...是的,你绝对正确,这个例子很清楚......但我有点困惑......假设我的“关于”类是我的例子中的Parent,我想将数据传递给孩子..然后我会像这样发送 对吗? –
在'About'的渲染方法中,你需要调用'',是的。然后在'People'组件中,您可以将人员作为'this.props.people'访问。 –