spaceWalker
spaceWalker

Reputation: 159

React Hook useEffect I can't get the data

I want to get real time bitcoin information but datas not coming. I get this error = React Hook useEffect has a missing dependency: 'coinData'. Either include it or remove the dependency array

 const [coinData,setCoinData] = useState([]);
  
  useEffect(() => {
    const getData = async () =>{
      const baseURL = "https://api.coingecko.com/api/v3/coins/bitcoin?tickers=true&market_data=true&community_data=true&developer_data=true&sparkline=true"
      const response = await axios(baseURL)
      setCoinData(response);
      console.log(coinData)
    }
  getData();
  }, []);

Upvotes: 0

Views: 200

Answers (1)

Denis Yakovenko
Denis Yakovenko

Reputation: 3535

The error is because you're using coinData (state) inside useEffect.

If you add coindData to the dependencies array, you'll get an infinite loop.

To log the response use console.log(response), not console.log(coinData).

  useEffect(() => {
    const getData = async () =>{
      const baseURL = "https://api.coingecko.com/api/v3/coins/bitcoin?tickers=true&market_data=true&community_data=true&developer_data=true&sparkline=true"
      const response = await axios(baseURL)
      setCoinData(response);
      console.log(response);
    }
    getData();
  }, []);

Upvotes: 1

Related Questions