I understand that there is a initial state of the data i.e 'null' before fetching the data from API call. But when API is called the data should update , instead log shows me 2 null values before logging actual data.
const [error, setError] = useState(null);
const [isLoaded, setIsLoaded] = useState(false);
const [data, setData] = useState(null);
const { user } = isAuthenticated();
useEffect(() => {
getUser(user.userId)
.then()
.then(
(result) => {
setIsLoaded(true);
setData(result);
},
(error) => {
setIsLoaded(true);
setError(error);
}
)
}, [])
console.log(data)
Why am I getting these null values before actual data? How to avoid these null values? Those initial null values are causing errors if I try to show that value on page.