0

Here is the Radar chart that I want to update.

enter image description here

Currently, I'm working on an application where once we search for a doctor, the radar chart that's being shown in the picture above should get updated based on the new data received through props. However, what's happening currently is that the chart isn't getting updated immediately even though the text data on the side is getting updated. The chart gets updated only when I refresh the page.

Here is the code for the entire Name component that you see in the picture. As you can see in the code, I'm directly passing the data from the props to the UniversalChart component.

    class Name extends Component {

state = {
    page_id_name: 1,
    hcp_id: 101,
    nameData: []
};


render() {


    console.log('data inside hcp_overview is: ', this.props)

    return (
        <div>
            <hr style={{ backgroundColor: "light-grey" }} />
            <div class="row m-b-lg m-t-lg" >
                <div class="col-md-3">
                    <div >
                        <div>
                            <h2 class="no-margins">
                                {this.props.data[101].overview.name}
                            </h2>
                            <h4>{this.props.data[101].overview.designation}</h4>
                            <h5>{this.props.data[101].overview.specialty}</h5>
                            <h5>{this.props.data[101].top_three_affiliations[0]}</h5>
                        </div>
                    </div>
                </div>
                <div class="col-md-3" >
                    <h5 style={{ marginLeft: 10 }}>
                        <u>Key Details</u>
                    </h5>
                    <table class="table small m-b-xs" style={{ fontSize: 12 }}>
                        <tbody>
                            <tr>

                                <td>
                                    <strong>NPI: </strong>{this.props.data[101].overview.npi}
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <strong>Gender: </strong>{this.props.data[101].overview.gender}
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <strong>Age: </strong>{this.props.data[101].overview.age}
                                </td>

                            </tr>

                        </tbody>
                    </table>
                </div>
                <div class="col-md-3" >
                    <h5 style={{ marginLeft: 10 }}><u>Top Affiliations</u></h5>
                    <table class="table small m-b-xs" style={{ fontSize: 12 }} >
                        <tbody>
                            <tr>

                                <td>
                                    {this.props.data[101].top_three_affiliations[0]}
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    {this.props.data[101].top_three_affiliations[1]}
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    {this.props.data[101].top_three_affiliations[2]}
                                </td>

                            </tr>

                        </tbody>
                    </table>
                </div>
                <div class="col-md-3" >
                    <UniversalChart
                        type={this.props.data[101].chart_data_config.chartType}
                        data={this.props.data[101].datasets}
                        options={this.props.data[101].chart_data_config}
                        labels={this.props.data[101].labels}
                        key={this.props.data[101].datasets}
                    />
                </div>

            </div>

        </div>
    )
}

And here is my UniversalChart component which uses ChartJS to display the chart.

   class UniversalChart extends Component {
state = {
    
}

chartRef = React.createRef();


componentDidMount() {

    const myChartRef = this.chartRef.current.getContext("2d");

    

    new Chart(myChartRef, {
        type: this.props.type,
        data: { labels: this.props.labels, datasets: this.props.data },
        options: JSON.parse(this.props.options.chartOptions)
    })
}

render() {
    return <canvas ref={this.chartRef} height={this.props.height} />
}

}

Am I missing something with regards to the React Component Lifecycle method? Just to reiterate, all the data on the right side gets updated but the chart itself doesn't get updated. Please let me know if any additional information needs to be added.

Here is the edited Universal Chart component where I tried the suggestion of @Giovanni Esposito.

   class UniversalChart extends Component {
state = {
    
}

chartRef = React.createRef();


componentDidUpdate(prevProps, prevState) {
    if(prevProps.data !== this.props.data){
        const myChartRef = this.chartRef.current.getContext("2d");

        new Chart(myChartRef, {
            type: this.props.type,
            data: { labels: this.props.labels, datasets: this.props.data },
            options: JSON.parse(this.props.options.chartOptions)
        })

    }
}


componentDidMount() {

    const myChartRef = this.chartRef.current.getContext("2d");
    

    new Chart(myChartRef, {
        type: this.props.type,
        data: { labels: this.props.labels, datasets: this.props.data },
        options: JSON.parse(this.props.options.chartOptions)
    })

}

render() {
    console.log('props inside render of universal chart is: ', this.props);
    console.log('state inside render of univ chart is: ', this.state);
    return <canvas ref={this.chartRef} height={this.props.height} />
}

  }
ShridharK
  • 365
  • 2
  • 14
  • Please let me know if anyone has any questions with regards to the code. Thanks. – ShridharK Apr 26 '21 at 07:08
  • 1
    At first sight seems that when props will be updated you are not updating your chart. To update your chart you should use `componentDidUpdate` event. Look here https://developmentarc.gitbooks.io/react-indepth/content/life_cycle/update/postrender_with_componentdidupdate.html – Giovanni Esposito Apr 26 '21 at 08:02
  • I tried componentDidUpdate but it was showing a weird behavior. The chart gets updated with the correct data but when I hover some parts of the chart, it flips back to represent the previous data. When I hover over the other parts of the chart, it again flips back to represent the correct present data. – ShridharK Apr 26 '21 at 11:24
  • Can you add a line where data is updated? – ulou Apr 26 '21 at 11:34
  • I have edited my universal chart component code to indicate where I tried ComponentDidUpdate. – ShridharK Apr 26 '21 at 11:52
  • Eventually, I have found a solution by doing the api request inside my Name component and then passing the data down to UniversalChart component instead of getting the data from a parent component. So instead of this.props.data...., the code will have this.state.nameData while passing data to the UniversalChart component. However, if anyone still knows of a solution with the earlier method, please let me know. – ShridharK Apr 26 '21 at 11:54
  • @ShridharK the strange behaviour you see could be because this condition `prevProps.data !== this.props.data` will be evaluated always as `true`. Try to make your comparer by using a customized function. – Giovanni Esposito Apr 26 '21 at 12:06
  • Why will it always be true? And I'm not sure how to write a customized function. – ShridharK Apr 26 '21 at 13:05
  • @ShridharK look [this](https://www.samanthaming.com/tidbits/33-how-to-compare-2-objects/) article. – Giovanni Esposito Apr 26 '21 at 15:43

0 Answers0