0

I am developing a React app which has ReCharts for creating bargraphs. Besides showing in the screen I want to export the graphic to a file to store in database.

As I saw in other question here, to export a chart I can do:

let chartSVG = ReactDOM.findDOMNode(this.currentChart).children[0];
let svgBlob = new Blob([chartSVG.outerHTML], {type: "text/html;charset=utf-8"});

However I am failing to insert the DOM node name of the chart (this.currentChart). The way the other question uses doesn't work for me:

Uncaught TypeError: Cannot set property 'currentChart' of undefined

I tried the component name inside findDOMNode, but got error:

Uncaught Error: Element appears to be neither ReactComponent nor DOMNode

My graph component is in another file and is declared inside my top level render() function as below:

render () {
  return(
    <div ...
      ...
      <br />
        <MyBarChart 
            id="currentChart" 
            ref={(chart) => this.currentChart = chart}
            data={this.props.dataGraph}
         />
      <br />
      ...
    </div>
  );
}

How do I call ReactDOMNode to be able to export the chart?

dpetrini
  • 1,169
  • 1
  • 12
  • 25

2 Answers2

0

The ref you're using will return the entire DOMElement to this.currentChart, furthermore you must ensure you're attempting to save the graph after the MyBarChart component has finished mounting, only then will it return its ref.

Further, is MyBarChart connected with Redux?

If so, the ref will need to be unwrapped as...

this.currentChart.getWrappedInstance()
Joshua Underwood
  • 919
  • 4
  • 14
  • Still getting the error: Uncaught TypeError: Cannot set property 'currentChart' of undefined . No Redux in this part. It is called by a button, so after mounting. – dpetrini Nov 08 '17 at 18:31
  • What is the context you are trying to set currentChart of? "Cannot set property of undefined" means that the object is undefined, in your case THIS is what is undefined. In your ref before you set this.currentChart do an `alert(this);` and ensure you get [Object object] – Joshua Underwood Nov 08 '17 at 18:38
0

You're scoping THIS to the ref function

change

ref={(chart) => this.currentChart = chart}

to

ref={(chart) => { this.currentChart = chart; } }
Joshua Underwood
  • 919
  • 4
  • 14