I have React component which renders D3 tree. The code snippet is given below.
componentDidMount()
{
var mountNode = ReactDom.findDOMNode(this.tree);
// Render the tree usng d3 after first component mount
if (this.props.treeData)
{
renderTree(this.props.treeData, mountNode, this.props.nodeName);//renderTree is Javascript function
}
}
contextmenu(node)
{
this.setState = {
style_popup: {
top: d3.event.clientY,
left: d3.event.clientX,
position: 'absolute'
},
render_on_click: true
}
}
render()
{
// Render a blank svg node
return (
<div id="tree">
<div id="tree-container" ref={(tree) =>
{
this.tree = tree;
}}>
</div>
{
(this.state.render_on_click) ? <PopUp popup_style={this.state.style_popup}/> : null
}
</div>
);
}
Inside renderTree()
(Not a React function) I have the following code snippet:
function renderTree(this.props.treeData, mountNode, this.props.nodeName)
{
//Some code.
.on('click',contextmenu);
}
I know this is the wrong way of calling contextmenu of React from Js, but how will I achieve this? I tried using refs
<D3Tree ref={instance => { this.D3tree = instance; }} treeData={this.props.treeData} />
but the D3Tree component is called from a different file which is the reason I am getting
this.D3Tree is undefined.
How should I call contextmenu which is a React function?