简体   繁体   中英

Reactjs, Typescript - property does not exist on child component

I'm using typescript 2.3.4 with React. I'm getting the error TS2339: error TS2339: Property 'name' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'. The error happens when I try to declare the property in a child component. How do I reference the property, properly in the child component?

For some reason the code is not executing in script runner.

any help is appreciated.

 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> 

You forgot to declare name as a React property in the ProfileData class definition. Something like this should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM