useEffect and useState hooks unique issue. No Data on first load

I’m just hacking at / learning react. Here is the deal. This works, you click the button, it runs the refreshhandler and boom state gets updated and renders new data on the page like magic. But what if you are just loading the page the first time? Well you would want the data to be there already right?

So I useEffect with an empty [] array trailing it so it will only run on the initial load.. and I have it call refreshHandler(); THIS ALSO WORKS! I can see it in the console the numbers, the datas, they are here!!!! But…. the page…. does not render the new data…… and now I’m past being mad, sad, rage, and a failure.

I come here… to stackoverflow…. for answers… and abuse.

import "./Card.css";
import { Row, Col, Container, Button } from "react-bootstrap";
import { useState, useEffect } from "react";

const Card = (props) => {
  const coinData = props.tickerSymbol;
  const [coins, setCoins] = useState([props.tickerSymbol]);
  const classes = "card " + props.className;

  const refreshHandler = async () => {
    coinData.forEach((coin) => {
      const ApiUrl = `${}&vs_currencies=usd`;
      var xhr = new XMLHttpRequest();

      xhr.addEventListener("load", () => {
        // console.log(xhr.responseText);
        let response = JSON.parse(xhr.responseText);
        let price = {};

        for (var r in response) {
          price = response[r];
          coin.price = price.usd;
      });"GET", ApiUrl);
    const ImCrazy = coinData;

  useEffect(async () => {
    await refreshHandler();
  }, []);

  return (
    <div className={classes}>
          <Col xs={3}>
          <Col xs={3}>
          <Col xs={3}>
          <Col xs={3}>
        { => {
          return (
              <Col xs={3}>{}</Col>
              <Col xs={3}>{coins.amount}</Col>
              <Col xs={3}>{coins.price}</Col>
              <Col xs={3}>{(coins.amount * coins.price).toFixed(2)}</Col>
              {console.log(coins.price + "  I'm in the render ")}
      <br />
        onClick={() => {
export default Card;

Please tell me what I am to blind to see. I beg of you.

Source: JavaSript – Stack Overflow

November 30, 2021
Category : News
Tags: javascript | react hooks | reactjs

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.