Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 623
Jimi Avatar asked Jun 11 '17 22:06

Jimi


1 Answers

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> {
like image 170
Oblosys Avatar answered Nov 17 '22 17:11

Oblosys