Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
React and Web Component ()
function getEventTarget(nativeEvent) {
var target = || nativeEvent.srcElement || window;
// If encapsulated in a Web Component
// use the Event path
if(nativeEvent.path && nativeEvent.path[0]) {
return nativeEvent.path[0];
// Normalize SVG <use> element events #4963
if (target.correspondingUseElement) {
target = target.correspondingUseElement;
// Safari may fire events on text nodes (Node.TEXT_NODE is 3).
// @see
return target.nodeType === 3 ? target.parentNode : target;
<!DOCTYPE html>
<meta charset="utf-8">
<title>Basic Example with WebComponents</title>
<link rel="stylesheet" href="../shared/css/base.css" />
<h1>Basic Example with WebComponents</h1>
<div id="container">
To install React, follow the instructions on
<a href="">GitHub</a>.
If you can see this, React is <strong>not</strong> working right.
If you checked out the source from GitHub make sure to run <code>grunt</code>.
<br /><br />
<h4>Example Details</h4>
This example demonstrates WebComponent/ReactComponent interoperability
by rendering a ReactComponent, which renders a WebComponent, which renders
another ReactComponent in the WebComponent's shadow DOM.
Learn more about React at
<a href="" target="_blank"></a>.
<script src=""></script>
<script src="../../build/react.js"></script>
<script src="../../build/react-dom.js"></script>
<script src=""></script>
<script type="text/babel">
function exampleEventHandler(e) {
console.log('Hello', e);
// Define WebComponent
var proto = Object.create(HTMLElement.prototype, {
attachedCallback: {
value: function() {
var mountPoint = document.createElement('span');
var name = this.getAttribute('name');
ReactDOM.render(<button onClick={exampleEventHandler}>{name}</button>, mountPoint);
document.registerElement('x-search', {prototype: proto});
// Define React Component
class HelloMessage extends React.Component {
render() {
return <div>Hello <x-search name={} />!</div>;
// Mount React Component (which uses WebComponent which uses React)
var container = document.getElementById('container');
ReactDOM.render(<HelloMessage name="Jim Sproch" />, container);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.