繁体   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