<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amit Shah on Medium]]></title>
        <description><![CDATA[Stories by Amit Shah on Medium]]></description>
        <link>https://medium.com/@amwam?source=rss-6d61eb31f578------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uaAOXTdEV5yvWEzmWtqUbg.jpeg</url>
            <title>Stories by Amit Shah on Medium</title>
            <link>https://medium.com/@amwam?source=rss-6d61eb31f578------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 00:24:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amwam/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using React with Web Components]]></title>
            <link>https://medium.com/@amwam/using-react-with-web-components-da9c733ee08b?source=rss-6d61eb31f578------2</link>
            <guid isPermaLink="false">https://medium.com/p/da9c733ee08b</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Amit Shah]]></dc:creator>
            <pubDate>Sun, 02 Jul 2017 21:07:39 GMT</pubDate>
            <atom:updated>2017-07-03T07:07:09.023Z</atom:updated>
            <content:encoded><![CDATA[<p>Dealing with legacy code can be tough, even more so if you want to slowly migrate your front end to React.</p><p>Recently I found myself in a situation where some HTML was rendered server side, and inserted into the browser on request. I wanted to use a shiny new React component, instead of falling back to the legacy jQuery code.</p><p>In comes <a href="https://www.webcomponents.org/">Web Components</a>!</p><p>Web Components allow you to write your own HTML tags, and use them through out your application. This solves my problem, by having the server render the new HTML tag, and the front end automatically load the required JavaScript to render/control it. When rendering the Web Component we can delegate the actual rendering to React.</p><p>Lets start with a simple React component:</p><pre>import React from &quot;react&quot;;<br><br>class MyComponent extends React.Component {<br>  render() {<br>    return &lt;div&gt;Hello world! This was rendered using React!&lt;/div&gt;;<br>  }<br>}</pre><p>In order to render the component, we have to create a new HTMLElement as so:</p><pre>class CustomHTMLElement extends HTMLElement {<br>  connectedCallback() {<br>    ReactDOM.render(&lt;MyComponent /&gt;, this);<br>  }<br>}</pre><p>In this, when the component is loaded by the browser, the connectedCallback will be called, allowing us to render the new component, at this (the location of the element in the DOM).</p><p>To finish, we just have to register the element with the customElements API:</p><pre>customElements.define(&quot;x-test&quot;, CustomHTMLElement);</pre><p>Now we can just use the tag &lt;x-test/&gt; in our HTML, to render the React component!</p><p>One problem with this setup can be <a href="https://babeljs.io/">Babel</a>, as it doesn’t play nice with transforming classes and extending HTMLElement. We can work around this using a custom class, and extending the prototype of HTMLElement:</p><pre>function BabelHTMLElement() {<br>  const newTarget = Object.getPrototypeOf(this).constructor;<br>  return Reflect.construct(HTMLElement, [], newTarget);<br>}<br><br>Object.setPrototypeOf(BabelHTMLElement.prototype, HTMLElement.prototype);<br><br>class CustomHTMLElement extends BabelHTMLElement {<br>...</pre><p>Now our CustomHTMLElement extends the BabelHTMLElement rather the HTMLElement directly.</p><p>To view an example project with this in use, you can checkout the repo here: <a href="https://github.com/Amwam/react-webcomponents">https://github.com/Amwam/react-webcomponents</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da9c733ee08b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>