Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use console.log in react

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)

1 Answers

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]);
like image 158
Drew Reese Avatar answered Sep 19 '25 18:09

Drew Reese