Sunday 1 September 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...