![38% of CISOs Fear They’re Not Moving Fast Enough on AI](https://cdn.sanity.io/images/cgdhsj6q/production/faa0bc28df98f791e11263f8239b34207f84b86f-1024x1024.webp?w=400&fit=max&auto=format)
Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
react-web-component
Advanced tools
Create Web Components with React
react-web-component-style-loader
react-router
React Web Component is available as the react-web-component
package on npm.
yarn add react-web-component
To create a web component with react-web-component
, simply pass a React component as the first parameter to ReactWebComponent.create
and the name of the web component you would like to create as the second parameter.
import React from 'react';
import ReactWebComponent from 'react-web-component';
class App extends React.Component {
render() {
return <div>Hello World!</div>;
}
}
ReactWebComponent.create(<App />, 'my-component');
Then in your HTML simply use your web component, in this case named my-component
.
<my-component></my-component>
Note that react-web-component
does not limit you in the complexity of your React component. You can pass an entire single page application in your web component if you need to.
react-web-component-style-loader
The challenge of adding CSS to your web component is that (as compared to a regular React component) you cannot simply put your CSS anywhere in your site, you need to inject it into the shadow dom of your web component, while at the same time you'll still want to use state of the art tooling (i.e. webpack) and create component based (S)CSS files.
We've got you covered. You can use the react-web-component-style-loader webpack loader module that in combination with react-web-component
will inject the css imported anywhere in your project into your web component. Here is a quick example:
App.js
import React from 'react';
import './app.css';
export default class App extends React.Component {
render() {
return <div>Hello World!</div>;
}
}
index.js
import React from 'react';
import ReactWebComponent from 'react-web-component';
import App from './App';
import './index.css';
ReactWebComponent.create(<App />, 'my-component');
Using the react-web-component-style-loader
both the CSS from app.css
as well as index.css
will end up in your web component. Read more about it in the react-web-component-style-loader repository.
ReactWebComponent.create(<App />, 'my-component', { style: '' });
ReactWebComponent.create(<App />, 'my-component', { cssFile: '' });
react-router
react-web-component
works with react-router
with one restriction: Since web components live within a host website you should now modify the URL of the website when the web component does internal routing. Luckily react-router
comes with an in memory router that does not alter the URL and keeps the state of the router internally.
import React from 'react';
import {
MemoryRouter as Router,
Route,
Link,
withRouter
} from 'react-router-dom';
import ReactWebComponent from 'react-web-component';
import { Home, About, Topics } from './components';
const App = withRouter(({ history}) => (
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/topics">Topics</Link></li>
</ul>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
<Route path="/topics" component={Topics}/>
</div>
))
const RoutedApp = () => (
<Router initialEntries={[ '/' ]}>
<App />
</Router>
);
ReactWebComponent.create(<RoutedApp />, 'my-routed-component');
If you encounter that an event that you rely on will not get triggered this is probably our fault (or Facebook's to be precise). React does not catch events inside shadow dom. A bug ticket has been filed for this at react#10422. For now, we have created a workaround for this in the shape of another npm package that this library depends on. In this library we listen for events on the DOM level and pass them on to React. If an event does not get triggered it probably means we have forgotten it in the list of events that we subscribe to. This is very inconvenient but not a problem. We can simply add it. Open an issue or a PR at WeltN24/react-shadow-dom-retarget-events and we will gladly add the event.
![]() Lukas Bombach |
FAQs
Create Web Components with React
The npm package react-web-component receives a total of 0 weekly downloads. As such, react-web-component popularity was classified as not popular.
We found that react-web-component 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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.