Rencere
Rencere

Reputation: 3

React js and Axios

I am trying to get this to display the price of Ethereum, but I cannot get it to display.

Can someone please help me? What should I change to get it to work?

Thanks!

import axios from "axios";

const Crypto = () =>{
    const [post, setPost] = React.useState([]);

    useEffect(() =>{
        axios.get('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=aud')
        .then ((response) => {
            setPost(response.data)
        })
            .catch(error => console.log('error'));
    }, []);

    return(
    <div>
        <h1> Test </h1>
        <h1>{post.ethereum}</h1>
    </div>
    )
}
export default Crypto;

Upvotes: 0

Views: 65

Answers (1)

Asif vora
Asif vora

Reputation: 3359

You need to import useEffect from React.

import React, { useEffect, useState } from "react";
import axios from "axios";

const App = () => {
  const [post, setPost] = useState([]);

  useEffect(() => {
    axios
      .get(
        "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=aud"
      )
      .then((response) => {
        setPost(response.data);
      })
      .catch((error) => console.log("error"));
  }, []);

  return (
    <div>
      <h1> Test </h1>
      <h1>{JSON.stringify(post.ethereum)}</h1>
    </div>
  );
};
export default App;

Upvotes: 1

Related Questions