簡體   English   中英

子組件上不存在Reactjs,Typescript - 屬性

[英]Reactjs, Typescript - property does not exist on child component

我正在使用帶有React的typescript 2.3.4。 我收到錯誤TS2339:錯誤TS2339:屬性'name'在類型'Readonly <{children?:ReactNode; }>&Readonly <{}>'。 當我嘗試在子組件中聲明屬性時發生錯誤。 如何在子組件中正確引用該屬性?

由於某種原因,代碼沒有在腳本運行器中執行。

任何幫助表示贊賞。

 export interface person { name: string; age: number; } interface State { personArray: person[]; } interface Props { } class ProfileData extends React.Component<{}, person> { public render() { return ( <section> <section> <h3>profile 1</h3> <div>{this.props.name}</div> </section> </section> ) } } export class Profile extends React.Component<Props, State> { public state: State; public props: Props; constructor(props: Props){ super(props); this.state = { personArray: [ { name: 'bazaaa', age: 42 }, { name: 'louis', age: 24 } ] }; } public render() { let profile1 = this.state.personArray[0]; return ( <ProfileData name={profile1.name} /> ) } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 

您忘記在ProfileData類定義中將name聲明為React屬性。 這樣的事情應該有效:

class ProfileData extends React.Component<{name: string}, person> {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM