<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sivakumar Kailasam</title>
    <description>The latest articles on DEV Community by Sivakumar Kailasam (@sivakumar_kailasam).</description>
    <link>https://dev.to/sivakumar_kailasam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F15879%2F9cbdb394-89e4-4e25-9f63-d22a9ecf5e80.jpeg</url>
      <title>DEV Community: Sivakumar Kailasam</title>
      <link>https://dev.to/sivakumar_kailasam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sivakumar_kailasam"/>
    <language>en</language>
    <item>
      <title>Using React components in your Ember app</title>
      <dc:creator>Sivakumar Kailasam</dc:creator>
      <pubDate>Fri, 23 Mar 2018 19:56:59 +0000</pubDate>
      <link>https://dev.to/sivakumar_kailasam/using-react-components-in-your-ember-app-41m9</link>
      <guid>https://dev.to/sivakumar_kailasam/using-react-components-in-your-ember-app-41m9</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3p3m2tfhpqrx2fclnrzb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3p3m2tfhpqrx2fclnrzb.png" width="800" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Disclaimer:&lt;/em&gt; I’m a member of the ember.js learning team. This isn’t a react vs ember article. They’re both awesome.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What if a team working with Ember wants to reuse an internal component built by a React team? Or maybe you know and appreciate multiple front end tool sets? This article for them and for you, oh open-minded developer!&lt;/p&gt;

&lt;p&gt;This is based on my team’s experience when working with an enterprise client who has these changes in production for about six months now. The only factor to watch out for is the bundle size by ensuring that duplicates of React libraries aren’t included the app.&lt;/p&gt;

&lt;p&gt;Let’s start off by making our Ember project aware of JSX syntax and give it the power to compile JSX code. Run the following command in your Ember project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev babel-plugin-transform-class-properties babel-plugin-transform-react-jsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In your ember-cli-build.js file, make the following changes:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Next, we’ll need to enable &lt;a href="https://eslint.org/" rel="noopener noreferrer"&gt;eslint&lt;/a&gt; to identify JSX code. Run the following command in your ember project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev eslint-plugin-babel eslint-plugin-react babel-eslint;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Make the following changes to your .eslintrc.js file,&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Add React &amp;amp; React DOM to our project by running,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Then add the following changes to the ember-cli-build.js file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Adding these imports will add React and ReactDOM global objects to our app. This is important because any React library we’ll import going forward will be expecting these globals to be available for their usage.&lt;/p&gt;

&lt;p&gt;Let’s create vendor shims so that we can use es6 import syntax for these libraries. We’re doing this instead of using the amd transformation on the imports because the global objects aren’t created when you use the transformation.&lt;/p&gt;

&lt;p&gt;Run the following commands &amp;amp; replace the contents of the generated files with the ones from the gist shown below. Then import them in the ember-cli-build.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ember generate vendor-shim react ember generate vendor-shim react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Let’s create a base class that we can use to create our React component wrappers. The idea behind this is to render one React component inside one Ember component. Doing so will help keep these components &lt;a href="https://www.infoq.com/presentations/Simple-Made-Easy" rel="noopener noreferrer"&gt;simple&lt;/a&gt;. Create a file app/react-component.js with the following content.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;First let’s create the obligatory ‘&lt;em&gt;hello world’&lt;/em&gt; component by running ember g component hello-world with the content below,&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;That was easy 🙂. Notice how we’re passing the value “React” to the React component on line 8, this could be an Ember component property. Now moving on to a more complex sample.&lt;/p&gt;

&lt;p&gt;Let’s add &lt;a href="https://github.com/davidtheclark/react-aria-modal" rel="noopener noreferrer"&gt;react-aria-modal&lt;/a&gt; to our app. Run npm install --save @sivakumar-kailasam/react-aria-modal and then make the following changes to the ember-cli-build.js file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now that we have it available to our app, let’s create the wrapper component.&lt;/p&gt;

&lt;p&gt;ember g component aria-modal&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This sample shows a way to bind methods between the React &amp;amp; Ember components. We’re updating the title that’s passed to the React component from within the Ember component’s methods &amp;amp; rerendering the react component on updates.&lt;/p&gt;

&lt;p&gt;Notice in the recording below how the updates are immediately rerendered. That’s because these are incremental updates applied to the already rendered React component. Try the demo site linked at the end of this article to see for yourself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwqrkjgzjlmcv3lrxjy77.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwqrkjgzjlmcv3lrxjy77.gif" width="700" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All of this might’ve made it look easy for you to set out to do this on your own. But there’s an important factor I haven’t mentioned so far 😅.&lt;/p&gt;

&lt;p&gt;The React components that you want to import need to be available as UMD modules. You can learn about UMD and other module formats by reading &lt;a href="https://medium.freecodecamp.org/javascript-modules-a-beginner-s-guide-783f7d7a5fcc" rel="noopener noreferrer"&gt;https://medium.freecodecamp.org/javascript-modules-a-beginner-s-guide-783f7d7a5fcc&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I had to setup &lt;a href="https://rollupjs.org/guide/en" rel="noopener noreferrer"&gt;rollup.js&lt;/a&gt; on a fork of react-aria-modal for this demo app to work. You can refer to &lt;a href="https://github.com/davidtheclark/react-aria-modal/compare/master...sivakumar-kailasam:master" rel="noopener noreferrer"&gt;https://github.com/davidtheclark/react-aria-modal/compare/master...sivakumar-kailasam:master&lt;/a&gt; to find out what it might take to do so with rollup.&lt;/p&gt;

&lt;p&gt;If your React component project uses &lt;a href="https://webpack.js.org/" rel="noopener noreferrer"&gt;webpack&lt;/a&gt;, you can refer to &lt;a href="https://github.com/bvaughn/react-virtualized" rel="noopener noreferrer"&gt;https://github.com/bvaughn/react-virtualized&lt;/a&gt; for the webpack setup you’d need to generate multiple module formats as output.&lt;/p&gt;

&lt;p&gt;You can see the deployed app at &lt;a href="https://sivakumar-kailasam.github.io/react-integration-sample/" rel="noopener noreferrer"&gt;https://sivakumar-kailasam.github.io/react-integration-sample/&lt;/a&gt; &amp;amp; view the code shown in this blog at the repo below. Try viewing the app in both the Ember inspector &amp;amp; React dev tools for fun! 😎&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/sivakumar-kailasam/react-integration-sample" rel="noopener noreferrer"&gt;sivakumar-kailasam/react-integration-sample&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;EDIT:&lt;/em&gt; &lt;a href="https://medium.com/u/60acf8b09399" rel="noopener noreferrer"&gt;&lt;em&gt;Alex LaFroscia&lt;/em&gt;&lt;/a&gt; &lt;em&gt;published an experimental addon&lt;/em&gt; &lt;a href="https://github.com/alexlafroscia/ember-cli-react" rel="noopener noreferrer"&gt;https://github.com/alexlafroscia/ember-cli-react&lt;/a&gt; based on this article. This is why I ❤️the ember community!&lt;/p&gt;

&lt;p&gt;If you like this article, follow me on twitter &lt;a href="https://twitter.com/sivakumar_k/" rel="noopener noreferrer"&gt;@sivakumar_k&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ember</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
