Reactjs, Typescript - property doesn't exist on child component

I am using typescript 2.3.4 with React. I am getting error TS2339: Error TS2339: Property 'name' does not exist on type 'Readonly <{children ?: ReactNode; }> and Readonly <{}> '. The error occurs when I try to declare a property in a child component. How do I bind a property, correctly in a child component?

For some reason, the code is not being executed in the 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>
      

Run codeHide result


+3


source to share


1 answer


You forgot to declare name

as a React property in your class definition ProfileData

. Something like this should work:



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

      

+4


source







All Articles