最近我開始學習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'訪問。 –