Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@bem-react/di
Advanced tools
Dependency Injection (DI) allows you to split React components into separate versions and comfortably switch them in the project whenever needed, e.g., to make a specific bundle.
DI package helps to solve similar tasks with minimum effort:
npm i -S @bem-react/di
Note! This example uses @bem-react/classname package.
E.g., for a structure like this:
Components/
Header/
Header@desktop.tsx
Header@mobile.tsx
Footer/
Footer@desktop.tsx
Footer@mobile.tsx
App.tsx
First, create two files that define two versions of the App and use different sets of components: App@desktop.tsx
and App@mobile.tsx
. Put them near App.tsx
.
In each App version (App@desktop.tsx
and App@mobile.tsx
) we should define which components should be used.
Three steps to do this:
const registry = new Registry({ id: cnApp() });
registry.set('Header', Header);
registry.set('Footer', Footer);
export const AppNewVersion = withRegistry(registry)(AppCommon);
The files should look like this:
1. In App@desktop.tsx
import { cn } from '@bem-react/classname';
import { Registry, withRegistry } from '@bem-react/di';
import { App as AppCommon } from './App';
import { Footer } from './Components/Footer/Footer@desktop';
import { Header } from './Components/Header/Header@desktop';
const cnApp = cn('App');
const registry = new Registry({ id: cnApp() });
registry.set('Header', Header);
registry.set('Footer', Footer);
export const AppDesktop = withRegistry(registry)(AppCommon);
2. In App@mobile.tsx
import { cn } from '@bem-react/classname';
import { Registry, withRegistry } from '@bem-react/di';
import { App as AppCommon } from './App';
import { Footer } from './Components/Footer/Footer@mobile';
import { Header } from './Components/Header/Header@mobile';
const cnApp = cn('App');
const registry = new Registry({ id: cnApp() });
registry.set('Header', Header);
registry.set('Footer', Footer);
export const AppMobile = withRegistry(registry)(AppCommon);
Time to use these versions in your app dynamically!
If in App.tsx
your dependencies were static before
import React from 'react';
import { cn } from '@bem-react/classname';
import { Header } from './Components/Header/Header';
import { Footer } from './Components/Footer/Footer';
export const App = () => (
<>
<Header />
<Footer />
</>
);
Now the dependencies can be injected based on the currently used registry
with ComponentRegistryConsumer
import React from 'react';
import { cn } from '@bem-react/classname';
import { ComponentRegistryConsumer } from '@bem-react/di';
// No Header or Footer imports
const cnApp = cn('App');
export const App = () => (
<ComponentRegistryConsumer id={cnApp()}>
{({ Header, Footer }) => (
<>
<Header />
<Footer />
</>
)}
</RegistryConsumer>
);
with useComponentRegistry
(require react version 16.8.0+)
import React from 'react';
import { cn } from '@bem-react/classname';
import { useComponentRegistry } from '@bem-react/di';
// No Header or Footer imports
const cnApp = cn('App');
export const App = () => {
const { Header, Footer } = useComponentRegistry(cnApp());
return (
<>
<Header />
<Footer />
</>
);
};
So you could use different versions of your app e.g. for conditional rendering on your server side or to create separate bundles
import { AppDesktop } from './path-to/App@desktop';
import { AppMobile } from './path-to/App@mobile';
Components inside registry can be replaced (e.g. for experiments) by wrapping withRegistry(...)(App)
with another registry.
import { Registry, withRegistry } from '@bem-react/di';
import { AppDesktop } from './App@desktop';
import { HeaderExperimental } from './experiments/Components/Header/Header';
const expRegistry = new Registry({ id: cnApp() });
// replacing original Header with HeaderExperimental
registry.set('Header', HeaderExperimental);
// AppDesktopExperimental will call App with HeaderExperimental as 'Header'
export const AppDesktopExperimental = withRegistry(expRegistry)(AppDesktop);
When App
extracts components from registry DI actually takes all registries defined above and merges. By default higher defined registry overrides lower defined one.
If at some point you want to create registry that wan't be overrided just call the constructor with overridable: false
.
const boldRegistry = new Registry({ id: cnApp(), overridable: false });
FAQs
BEM React Dependency Injection
The npm package @bem-react/di receives a total of 95 weekly downloads. As such, @bem-react/di popularity was classified as not popular.
We found that @bem-react/di demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.