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> {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With