0

I have certain code as below:-

class BarChart extends Component {
  constructor(){
    super();
    this.state = {
      chartData:{}
    }
  }

  componentDidMount() {
    this.getChartData();
  }

  getChartData() {
    axios.get("http://localhost:5001/inventory/clusterscount").then(res => {
        const myresponse = res.data;
        console.log(myresponse)
        let countcluster = [];
        let listregion = [];
        for (const dataobj of myresponse){
            countcluster.push(parseInt(dataobj.clusterscount));
            listregion.push(dataobj.region);
        }
        console.log(countcluster)
        console.log(listregion)
        this.setState({
          chartData: {
            labels:listregion,
            datasets: [
              {
                label: "level of thiccness",
                data: countcluster,
                backgroundColor: ["rgba(75, 192, 192, 0.6)"],
                borderWidth: 4
              }
            ]
          }
        });
      });
    }

  render(){
        return (
            <div className="App">
              <h1>Dankmemes</h1>
              <div>
                <Line
                  data={this.state.chartData}
                  options={{
                    responsive: true,
                    title: { text: "THICCNESS SCALE", display: true },
                    scales: {
                      yAxes: [
                        {
                          ticks: {
                            autoSkip: true,
                            maxTicksLimit: 10,
                            beginAtZero: true
                          },
                          gridLines: {
                            display: false
                          }
                        }
                      ],
                      xAxes: [
                        {
                          gridLines: {
                            display: false
                          }
                        }
                      ]
                    }
                  }}
                />
              </div>
            </div>
          );
    
        }     
    }
export default BarChart;

Now while running it am getting the desired clusters and regions as below:-

0: {clusterscount: '2', region: 'test1'}
1: {clusterscount: '10', region: 'test2'}
2: {clusterscount: '8', region: 'test3'}
3: {clusterscount: '1', region: 'test4'}
4: {clusterscount: '8', region: 'test5'}
5: {clusterscount: '2', region: 'test6'}

I am able to get results for clustercount and listregion as well, but keep getting this error. I have tried multiple things but out of ideas.

But in logs am getting as below:- enter image description here

Can someone help me with this?

Sarvesh Agarwal
  • 153
  • 1
  • 1
  • 9
  • How do you use this component? The issue in general is about trying to use results of `fetch` to set state for something that no longer exists in your DOM. – raina77ow Jan 30 '22 at 10:20
  • Does this answer your question? [Can't perform a React state update on an unmounted component](https://stackoverflow.com/questions/53949393/cant-perform-a-react-state-update-on-an-unmounted-component) – raina77ow Jan 30 '22 at 10:21

1 Answers1

0

The react useEffect expects a cleanup function to cancel subscription and asynchronus tasks so we need to check if component is mounted or not there are couple of ways we can do it and react community have good solution for that.

const LineChart = () =>{
    const [chartData,setChartData]= useState({});
    const [myresponse, setmyresponse] =useState([]);
  const isMountedRef = useRef(null);
    useEffect(() =>{
    isMountedRef.current = true;
        let countcluster = [];
        let listregion = [];
        axios.get("http://localhost:5001/inventory/clusterscount").
        then(res=>{
           if(isMountedRef.current){
   const myresponse= res.data;
            setmyresponse(myresponse)
            console.log(myresponse);
         for (const dataobj of myresponse){
             countcluster.push(parseInt(dataobj.clusterscount));
             listregion.push(dataobj.region);
         }
         setChartData({
            labels: listregion,
            datasets: [
              {
                label: "level of thiccness",
                data: countcluster,
                backgroundColor: ["rgba(75, 192, 192, 0.6)"],
                borderWidth: 4
              }
            ]
          });
     
        }
        
         
        })
    .catch(err=>{
        console.log(err);
    });
    return () => isMountedRef.current = false;
    },[])

abhi patil
  • 504
  • 2
  • 11
  • Thanks i tried this, but it has stopped doing console.log too now. – Sarvesh Agarwal Jan 30 '22 at 10:37
  • that's unexpected, we're not doing anything fancy here just checking if component is mounted or not. can specify which console log you;re not able to log. and if the solution suffice you're problem please marked it as answered – abhi patil Jan 30 '22 at 12:46