Skip to content

Instantly share code, notes, and snippets.

@hipertracker
Created December 11, 2019 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hipertracker/282ea963cca5613f94ca9f3be27a7bb6 to your computer and use it in GitHub Desktop.
Save hipertracker/282ea963cca5613f94ca9f3be27a7bb6 to your computer and use it in GitHub Desktop.
React refs forwarding to custom element issue
import * as React from 'react'
import {Component} from 'react'
class App extends Component {
setSvgRef(ref) {
console.log('Parrent React got ref:', ref); // this works
}
render() {
return <ScatterPlot svgRef={e => this.setSvgRef(e)}/>
}
}
class ScatterPlot extends Component {
render() {
return (
<div>
<pre>A Table</pre>
<svg ref={e => {
console.log('React run'); // this works
this.props.svgRef(e);
}
}/>
</div>
)
}
}
export default App;
class CustomElement extends HTMLElement {
connectedCallback() {
const mountPoint = document.createElement('span');
this.attachShadow({mode: 'open'}).appendChild(mountPoint);
this.update();
}
setSvgRef(ref) {
console.log('Custom element got ref -> ', ref) // PROBLEM: never reach to that point :(
}
update() {
render(<ScatterPlot svgRef={e => {
console.log('Custom Element run'); // PROBLEM: this does not run :(
this.setSvgRef(e)
}}/>, this.mountPoint);
}
}
customElements.define('scatter-plot', CustomElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment