What is the difference between writing a variable after the render function like this :
render() {
var headers=[
{ key: 'userId', label: 'User ID' },
{ key: 'id', label: 'ID' },
{ key: 'title', label: 'Title' },
{ key: 'body', label: 'Body' }
];
return (
and writing " this.name of variable " in the constructor function like this :
class App extends Component {
constructor(props) {
super(props);
this.state = {
users: []
};
this.headers = [
{ key: 'userId', label: 'User ID' },
{ key: 'id', label: 'ID' },
{ key: 'title', label: 'Title' },
{ key: 'body', label: 'Body' }
];
}
componentDidMount() {
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => {
return response.json();
}).then(result => {
this.setState({
users:result
});
});
}
render() {
Other than the fact that when i summon it in the first one i write { headers } and in the second one i write {this.headers}
Note:This is not about var vs this,It's about the structure of the main app class in the create-react-app and its connection with the position in which the previous code is written.