I have the below component and I have set the initial state as users:null
then I am passing the state to my componentDidMount()
as below code
import React, { Component } from "react";
export default class Users extends Component {
constructor(props) {
super(props);
this.state = {
users: null
};
}
componentDidMount() {
fetch("https://jsonplaceholder.typicode.com/users")
.then((response) => response.json())
.then((data) =>
this.setState({
users: data
})
);
console.log(this.state.users);
}
render() {
return (
<div>
<h1>Users</h1>
</div>
);
}
}
From the above component I am getting a null in the console.
Can I know where it went wrong newbie to reactjs
Thanks in advance