Sunday, September 1, 2019

reactjs - this.state undefined in a method

I have a custom component that requires access to one of my actual component state (rows).
I defined it in my constructor but when it should be rendered, this.state seems to be always empty, and i dont know why...



The console always displays "undefined this.state".



Here is my code :



export class TableContainer extends React.Component {
constructor(props : any, context: any) {
super(props,context);

this.state = {
rows:[
{ id: 1, title: 'John', count: 32 },
{ id: 2, title: 'Carl', count: 18 },
{ id: 3, title: 'Mike', count: 25 }
]
};
}

rowGetter(i: number) {

if(this.state) {
console.log('defined this.state');
return this.state.rows[i];
}
else {
console.log('undefined this.state');
return {};
}
}


render() {
return (

columns={this.state.columns}
rowGetter={this.rowGetter}
rowsCount={this.state.rows.length}
minHeight={500}
/>


);
}
}


I'm new in react so probably it's a little mistake.
I have to precise that rowsCount get the good "this.state.rows.length" in the render function.



Thank you by advance

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...