1

I have created Barchart component (displays barchart) which is child component. In parent component I am passing props to Barchart component. In Barchart component I am using if/else and to render the Barchart accordingly.

Barchart component:

import React, { Component } from 'react';

class Barchart extends Component {

  constructor(props){
    super(props);
  }

  componentDidMount(){

    if(this.props.statType === 'batting'){

        const data1 = {
            labels: ['XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL'],
        series: [20, 60, 120, 200, 180, 20, 10]
        }

        const options1 = {
            width:300,
            height:300,
            distributeSeries: true
        }

        const mychart1 = new Chartist.Bar('.ct-bar-chart', data1,options1);

    }else if(this.props.statType === 'bowling'){

      const data1 = {
        labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
        series: [200, 600, 120, 200, 1800, 200, 100]
      }

      const options1 = {
        width:300,
        height:300,
        distributeSeries: true
      }

      const mychart1 = new Chartist.Bar('.ct-bar-chart', data1,options1);
    }
}
  render(){
    return(
         <div className="ct-bar-chart">
             {this.mychart1}
         </div>
    )}
}

export default Barchart;

Bowling component:

return(
      <div><Barchart bowldata={this.props} statType='bowling'/></div>
)

Batting component:

return(
      <div><Barchart batdata={this.props} statType='batting'/></div>
)

I am able to see barchart on batting page (i.e batting component) but on bowling page (i.e bowling component) nothing is displayed.

2
  • put everything in the render method Commented Jun 30, 2018 at 6:40
  • @niklas render() method is very large so I cannot put entire code. If possible can you share some working example on jsfiddle or sandbox. Commented Jun 30, 2018 at 6:44

1 Answer 1

1

Try using ref in while initializing graph rather than class. Consider below code:

render(){
    return(
         <div className="ct-bar-chart"
              ref={(ele) => this.chartElement = ele}>
              ...
         </div>
    )}

Now while initializing chart use:

const mychart1 = new Chartist.Bar(this.chartElement, data1,options1);

So the advantage here here is this.chartElement is unique for every component, while if you use class it can conflict with another component.

Sign up to request clarification or add additional context in comments.

12 Comments

@Hirday Woow it works now. I am newbie and just started learning reactjs. I want to know why does adding ref works ? I had mostly used props earlier and never tried to used ref.
@stonerock because ref has the element which is very specific to each component you are initializing while class on which you are initializing chart may conflict with class used in another component. Hence ref works better. I will add it in my answer too.
No both are specific to component. So here you are passing different data to 2 component instances but the css class on which chart is initialized is same for both of them hence we used ref.
@stonerock Have a look at When to use refs?. Also you can use the latest way of creating ref as mentioned here. I have used functional ref as i am used to it.
There is third point too there Integrating with third-party DOM libraries. Which is your use-case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.