I'm very new to React. I have a component that renders fine, when I try to initialise the state, it says It cannot find the name, as I have not declared it yet, so how do I initialise it properly? I'm using:
"react": "^15.5.4", "react-dom": "^15.5.4",
The component errors here on the line below:
export class Profile extends React.Component<{}, state> {
constructor(props){
super(props);
this.state = [{name: 'baz'}, {name: 'shaz'}];
}
public render() {
return (
<section>
<section>
<h3>profile 1</h3>
<div>baz</div>
</section>
<section>
<h3>profile 2</h3>
<div>shaz</div>
</section>
</section>
)
}
ReactDOM.render(
>> this is where I call <Profile />,
document.getElementById('app'),
)
Edit
So I managed to solve the issue with everyones help:
interface State {
name: string;
}
export class Profile extends React.Component<{}, State> {
public state: State;
constructor(props){
super(props);
this.state = {
name: 'baz111'
};
}
public render() {
return (
<section>
<section>
<h3>profile 1</h3>
<div>baz</div>
</section>
<section>
<h3>profile 2</h3>
<div>{this.state.name}</div>
</section>
</section>
)
}
}