use a React.js component as web component
npm install reactive-elementsYou should use this README, which refers to 0.10.0, the latest stable version on npm: https://github.com/PixelsCommander/ReactiveElements/blob/7cce3d7b472989878ac1433cec0e8168fd4136aa/README.md
``sh`
npm install reactive-elements
yarn add reactive-elements
Placing component in a pure HTML
`html`
React class definition
`jss argument
/ @jsx React.DOM /
MyComponent = React.createClass({
render: function() {
console.log(this.props.items); // passed as HTML tag
console.log(this.props.children); // original tag children
return (
ReactiveElements('my-react-component', MyComponent);
``
`js
import React, { Component } from 'react';
import ReactiveElements from 'reactive-elements';
class Welcome extends Component {
render() {
return
ReactiveElements('welcome-component', Welcome);
`
Original children of a custom element is injected to component as
this.props.children.
`html`
In this case this.props.children is equal to "Hello world".
Container node of the element is passed as this.props.container. BothdocumentFragment
props.container and props.children have type of .
An attribute that has the value "true" or "false" will be converted into thetrue
boolean or false when given to the React component:
`html`
Here, this.props.isLoggedIn === true within the React component.
If you don't want this behaviour you can disable it with a special attribute:
`html`
If you want to expose React component methods on custom element - assign them to
component as following:
`js`
componentDidMount: function() {
this.props.container.setTextContent = this.setTextContent.bind(this);
}
You may add attributeChanged callback to component in order to handle / modify
/ filter incoming values.
`js`
attributeChanged: function(attributeName, oldValue, newValue) {
console.log('Attribute ' + attributeName + ' was changed from ' + oldValue + ' to ' + newValue);
this.props[attributeName] = parseInt(newValue);
}
You may trigger DOM event from React component by using following snippet:
`js`
var event = new CustomEvent('change', {
bubbles: true,
});
React.findDOMNode(this).dispatchEvent(event);
Subscribing to DOM events is similar:
`js`
React.findDOMNode(this).addEventListener('change', function(e){...});
You can also specify options to the ReactiveElements call, e.g.
`js`
ReactiveElements('welcome-component', Welcome, options);
By default, your React element is rendered directly into the web-component root. However, by setting this option - your React element will instead be rendered in a Shadow DOM inside the web-component instead.
- React.js
- React DOM
- Custom elements support or
polyfill
- Support or polyfills for:
- regexp.matchregexp.replace
- object.define-setter
- object.define-getter
- object.define-property
- function.name
- web.dom.iterable
- array.iterator
- object.keys
- object.set-prototype-of
- reflect.construct
- function.bind
-
MIT: http://mit-license.org/
Copyright 2014 Denis Radin aka PixelsCommander
Inspired by Christopher Chedeaus
react-xtags