Am new to react, I have a fetch operation and I want to check data from api using console.log before using it just like in javascript but i can't figure out how to do it in react...Any help?
const [all, setAll]=React.useState([])
React.useEffect(()=>{
fetch('https://api.themoviedb.org/3/trending/all/day?api_key=key')
.then(data=>{
return data.json()
}).then(completedata=>{
setAll(completedata)
})
.catch((err) => {
console.log(err.message);
})
},[1])
console.log(all)
It's all still Javascipt, use console.log(data.results)
in the promise chain, or the all
state in an useEffect
hook with dependency on the all
state.
const [all, setAll] = React.useState([]);
React.useEffect(() => {
fetch('https://api.themoviedb.org/3/trending/all/day?api_key=key')
.then(data => {
return data.json();
})
.then(data => {
setAll(data.results);
console.log(data.results);
})
.catch((err) => {
console.log(err.message);
});
},[]);
or
const [all, setAll] = React.useState([]);
useEffect(() => {
console.log(all);
}, [all]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With