https://github.com/a7ul/react-webcomponentify
Build Web Components with React or Preact (without any extra effort)
https://github.com/a7ul/react-webcomponentify
javascript-library openlibrary react reactjs web-component webcomponents
Last synced: 9 months ago
JSON representation
Build Web Components with React or Preact (without any extra effort)
- Host: GitHub
- URL: https://github.com/a7ul/react-webcomponentify
- Owner: a7ul
- License: mit
- Created: 2019-01-26T15:37:51.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-07-15T02:07:57.000Z (almost 2 years ago)
- Last Synced: 2025-01-08T02:09:35.754Z (over 1 year ago)
- Topics: javascript-library, openlibrary, react, reactjs, web-component, webcomponents
- Language: TypeScript
- Homepage: https://a7ul.github.io/webcomponents-with-react-webcomponentify
- Size: 881 KB
- Stars: 69
- Watchers: 2
- Forks: 9
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# react-webcomponentify
[](https://badge.fury.io/js/react-webcomponentify)

**Build and export React Components as Web Components without any extra effort.**
Size = [~1.5kB after gzip](https://bundlephobia.com/package/react-webcomponentify)
_\* works nicely with preact aswell: See demo_
## Show me live demo?
- Demo Link:
- Demo source code (recommended):
### Table of Contents
- [Use cases](#use-cases)
- [Install](#install)
- [Usage](#usage)
- [Basic](#basic)
- [Advanced](#advanced)
- [Sending non string props to react](#sending-non-string-props-to-react)
- [What about child elements?](#what-about-child-elements)
- [TypeScript support](#typescript-support)
- [Maintainers](#maintainers)
## Use cases
- **Export existing react components as web components** so you can use them with Vue or Angular.
- **Use react's rich api to build web components** with state management, etc. Instruction on how to do exactly that and Live Demo here:
- Lets say you are writing a component library with web components but you already have a huge collection of component in react.You can use this library to generate a component library with the existing components. And then safely continue to rewrite each one of them behind the scene. This makes sure other teams are not waiting for you to finish.
- For more crazy people - You can even export your entire react app as a web component and embed it into another app made with Angular or Vue. So you can keep writing newer parts of code in react while keeping your legacy code working on the side.
- Maybe (not tried) you can embed another old react app (wrapped with this module) inside ur current react app.
## Install
```bash
npm install react-webcomponentify
```
or
```bash
yarn add react-webcomponentify
```
## Usage
### Basic
**Simple use case**
```js
import React from "react";
import { registerAsWebComponent } from "react-webcomponentify";
export const ExampleReactComponent = () => {
return
Hello ;
};
registerAsWebComponent(ExampleReactComponent, "example-component");
```
In HTML:
```html
....
....
```
### Advanced
#### Sending non string props to react
You can send serializable string props via the html attributes itself. But for props like callback functions or complex objects you can use the `setProps` method on the element as shown below.
```js
import React from "react";
import { registerAsWebComponent } from "react-webcomponentify";
export const ButtonComponent = props => {
return (
Hello {props.text}
);
};
registerAsWebComponent(ButtonComponent, "button-web");
```
In HTML:
```html
....
....
const myBtn = document.getElementById("mybutton");
myBtn.setProps({
onClick: () => console.log("btn was clicked")
});
```
Every custom component built using react-webcomponentify will have an instance method `setProps`
```js
element.setProps({
....
/* set the props here that you want to send to react */
....
})
```
#### What about child elements?
Thats possible too 😎
```js
import React from "react";
import { registerAsWebComponent } from "react-webcomponentify";
// You access the children just like you would in react (using this.props.children)
export const ComponentWithChild = props => {
return (
Hello World
{this.props.text}
{this.props.children}
);
};
registerAsWebComponent(ComponentWithChild, "component-with-child");
```
In HTML:
```html
....
Some child
....
```
This will send `
Some Child
` via this.props.children to the React component `ComponentWithChild`.
Note that `Some Child
` is a dom node and not a react component. So it will be wrapped with a simple react component found here: https://github.com/a7ul/react-webcomponentify/blob/master/src/react-dom-child.js
But for implementation purposed use it like a regular child component.
### TypeScript support
This library is written in TypeScript. All declarations are included.
## Maintainers