43

I have been using React and look to use Polymer tags inside of React. React does not recognize Polymer tags as React only handles basic DOM tags. Is there a way to add the Polymer tags to React DOM library?

James Montagne
  • 77,516
  • 14
  • 110
  • 130
Vu Dang
  • 721
  • 1
  • 7
  • 11
  • There are definitely people exploring web components (like Polymer) inside React. Stumbled across http://2015.jsday.it/talk/you-can-have-it-both-ways-using-web-components-in-a-react-ui/ -- see the slide ~2/3 in, titled "solution: a custom react class". No code there, but lays out the basic approach. – medmunds Aug 11 '15 at 22:55
  • Also see https://github.com/facebook/react/issues/2746 – medmunds Aug 11 '15 at 23:06
  • Vu Dang, maybe consider accepting a more updated answer? a.k.a - @FakeRainBrigand 's answer – zaxy78 Apr 17 '18 at 11:50

7 Answers7

55

Yes, it is possible.

  1. Create a polymer element.

    <link rel="import" href="../../bower_components/polymer/polymer.html">
    
    Polymer({
        is: 'calender-element',
        ready: function(){
            this.textContent = "I am a calender";
        }
    });
    
  2. Make the polymer component a html tag by importing it in a html page. E.g. import it in the index.html of your react application.

    <link rel="import" href="./src/polymer-components/calender-element.html">
    
  3. Use that element in the jsx file.

    'use strict';
    
    import React from 'react';
    
    class MyComponent extends React.Component{
        render(){
            return (
                <calender-element></calender-element>
            );
        }
    }
    
    export default MyComponent;
    
Highmastdon
  • 6,960
  • 7
  • 40
  • 68
Sanjeev
  • 855
  • 1
  • 9
  • 15
  • 3
    This is a better answer. The OP was looking for the ability to use Polymer tags inside React components. Here is a clear example of doing just not that. – chrisco512 Sep 21 '15 at 21:52
11

Is it possible to use Polymer inside of React?

Short answer: not really.

Long answer: kinda. You have to create components which directly create the nodes and manipulate attributes. There are also other considerations for children of the element, etc.

Is it possible to use React inside of Polymer?

It's pretty much the same answer this way, you'd have to wrap a React component in a polymer element.

Why?

Polymer (based on web components), and React (a ui component library), are both based on 'components'. Because there's no single way to express a component in web, you'll need to bridge between the various libraries. The same holds true for questions about 'react in angular', 'jquery plugin in react', 'knockout in jquery plugin', 'react in backbone', 'angular with polymer elements which use backbone and react with polymer elements which use angular', etc.

In a case like angular with polymer, you might think it's very simple, but polymer doesn't know about evaluating angular expressions, or any kind of declarative callbacks. You need a bridge in nearly every case, and they're never pretty.

Brigand
  • 84,529
  • 20
  • 165
  • 173
  • 12
    "polymer doesn't have the concept of data binding" - Is this a typo? Polymer totally has data binding https://www.polymer-project.org/docs/polymer/databinding.html – Peter Burns Oct 16 '14 at 15:54
  • 3
    WebComponents (i.e. like in Polymer) can be treated in ReactJS just like any other HTML element is. So React and Polymer can work well together. Polymer and WebComponents is just a standardized way of creating new HTML Tags, but those tags, once defined, are recognized at the Browser Level (albeit using polyfills currently), and so WebComponents can coexist with any and all web frameworks perfectly well. So to repeat, any WebComponent can be used just as if it were a native HTML tag, and do anything any other TAG does. It truly IS a new tag as far as the browser code can tell. –  Nov 18 '17 at 02:36
7

this is a fairly old question but how about https://www.npmjs.com/package/react-polymer ? isn't this a support of polymer components for react?

import reactPolymer from 'react-polymer'; //IMPORTANT: Must be imported before React.
import React from 'react';

reactPolymer.registerAttribute('raised');
reactPolymer.registerAttribute('url');
reactPolymer.registerEvent('response', 'onResponse');


<paper-button raised>another button</paper-button>
<iron-ajax url="http://example.com/" onResponse={this.handleResponse} />
tbo
  • 9,398
  • 8
  • 40
  • 51
3

Answer according to current stages of react and polymer

Since this question was asked a while ago and a lot has changed since then, I'd like to add that you can now use polymer elements in react directly but for your custom attributes and events it causes problem it can easily be handle by using react-polymer, It has support for almost all elements, with exception of gold-* elements.

Why would you want to use Polymer with react?

  1. It can further simplify your development process or make it a big mess. It depends on how you use it
  2. Speed of development and ease of use offered by polymer components is unrivaled.
  3. React can further break down your components comprising of polymer components, into manageable pieces.
  4. Simply because, react and JSX is love.
  5. Hey why the hell not??
Community
  • 1
  • 1
Syed Huzaifa Hassan
  • 776
  • 1
  • 6
  • 22
2

The answer is YES. But it is not straight forward. So, I tried following some documentations which are around in fact even the official one but the best was this: https://medium.com/jens-jansson/start-using-web-components-in-react-6ccca2ca21f9

I followed the steps mentioned and it worked! I am also mentioning the github repo wherein I tried to integrate the vaadin datepicker and also one of the polymer element paper-input. https://github.com/manit815/react-with-webcomponent

Manit
  • 1,087
  • 11
  • 17
  • Inside the JSX, the class attribute of web-components is somehow ignored. @MKant I have opened an issue https://github.com/manit815/react-with-webcomponent/issues/1 regarding thsi problem. – kryptokinght Feb 03 '19 at 19:28
0

Yes, you can use Polymer element inside react.

  1. Create Polymer element

    import { LitElement, html } from 'lit-element';
    export class CustomButton extends LitElement {
    static get properties() {
        return {
            isDisabled : { type: Boolean },
            buttonType: { type: String },
        };
    }
    
    constructor() {
        super();
        this.isDisabled = false;
        this.button = 'button';
    }
    
    render() {
        return html`
            <button>
                <slot></slot>
            </button>
        `;
       }
    }
    
    customElements.define('polymer-button', CustomButton);
    
  2. Import the element into an HTML file using <script type="module">. Use the import statement (as shown above) to import it from another ES6 module.

    <script type="module" src="./polymer-button.js">
    
  3. Once you've imported it, you can use a custom element just like you'd use a standard element.

    import React from 'react';
    export const PolymerButton = () => {
       return ( 
           <polymer-button /> 
        )
    }
    
-1

I just tried this today and I was able to successfully use the material elements from their element catalog. I haven't gotten around to testing it thoroughly, but as far as using the tags in React goes, it works and all the html and css is there.

To use existing elements, just follow their using elements guide.

Sam
  • 129
  • 2
  • 11