[Chartjs]-Line Chart data not showing

1👍

Usestate does not have a dependency array that automatically updates, so your chartData does never get updates. in your fetch you need to call setChartData already with the correct values.

React.useEffect(() => {
  fetch("http://localhost:3001/bitcoin")
    .then((res) => res.json())
    .then((data) => {

      const twoDimensionArr = data.message;
      setData(twoDimensionArr);
      setDates(twoDimensionArr.map(item => item[0]));
      setCoinValue(twoDimensionArr.map(item => item[1]));
      setChartData({
        labels: twoDimensionArr.map(item => item[0]),
        datasets: [{
          label: "value of BTC in ILS",
          data: twoDimensionArr.map(item => item[1]),
          backgroundColor: 'gold'
        }]
      })
    })
    .then(console.log(dates))
    .then(console.log(coinValue))
}, []);

Leave a comment