
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-2-webcomponent
Advanced tools
react-2-webcomponent converts React components to custom elements! It lets you share react components as native elements that don't require mounted being through React. The custom element acts as a wrapper for the underlying react component. Use these custom elements in any framework (vue, svelte, angular, ember, canjs) the same way you would use standard HTML elements.
react-2-webcomponent:
1.11KB minified and gzipped.Given a react component like:
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
Call reactToWebComponent and customElements.define as follows:
import reactToWebComponent from "react-2-webcomponent";
const WebGreeting = reactToWebComponent(Greeting, React, ReactDOM);
customElements.define("web-greeting", WebGreeting);
Now you can use <web-greeting> like any other HTML element!
You can create it programatically:
const webGreeting = document.createElement("web-greeting");
webGreeting.name = "StandardsFan";
document.body.append(webGreeting);
webGreeting.innerHTML //-> "<h1>Hello, StandardsFan</h1>"
Or you can use it declaratively:
document.body.innerHTML = "<web-greeting></web-greeting>";
document.body.firstChild.name = "CoolBeans";
document.body.firstChild.innerHTML //-> "<h1>Hello, CoolBeans</h1>"
By default, custom elements created by reactToWebComponent only
pass properties to the underlying React component. To make attributes
work, you must specify your component's properties with
PropTypes as follows:
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
Greeting.propTypes = {
name: PropTypes.string.isRequired
};
Now reactToWebComponent will know to look for name attributes
as follows:
document.body.innerHTML = "<web-greeting name='Amazed'></web-greeting>";
document.body.firstChild.innerHTML //-> "<h1>Hello, Amazed</h1>"
To install from npm:
https://github.com/satty1987/react-to-webcomponent
reactToWebComponent(ReactComponent, React, ReactDOM) takes the following:
ReactComponent - A react component that you want to
convert to a Web Component.React - A version of React (or preact-compat) the
component works with.ReactDOM - A version of ReactDOM (or preact-compat) that the component works with.A new class inheriting from HTMLElement is
returned. This class can be directly passed to customElements.define as follows:
customElements.define("web-greeting",
reactToWebComponent(Greeting, React, ReactDOM) );
Or the class can be defined and used later:
const WebGreeting = reactToWebComponent(Greeting, React, ReactDOM);
customElements.define("web-greeting", WebGreeting);
var myGreeting = new WebGreeting();
document.body.appendChild(myGreeting);
Or the class can be extended:
class WebGreeting extends reactToWebComponent(Greeting, React, ReactDOM)
{
disconnectedCallback(){
super.disconnectedCallback();
// special stuff
}
}
customElements.define("web-greeting", WebGreeting);
reactToWebComponent creates a constructor function whose prototype is a Proxy. This acts as a trap for any property set on instances of the custom element. When a property is set, the proxy:
Also:
props passed to the React component.FAQs
Convert react components to native Web Components.
We found that react-2-webcomponent demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.