
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
cheap-di-react
Advanced tools
Dependency injection based on cheap-di for using in React components with React Context
Integration of cheap-di into React via React.Context
npm i cheap-di-react
There is a simple logger.
// logger.ts
export abstract class Logger {
abstract debug(message: string): void;
}
export class SimpleConsoleLogger extends Logger {
debug(message: string) {
console.log(message);
}
}
export class ConsoleLoggerWithPrefixes extends Logger {
constructor(private prefix: string) {
super();
}
debug(message: string) {
console.log(`${this.prefix}: ${message}`);
}
}
Use it in the React component
// App.tsx
import { DIProvider } from 'cheap-di-react';
import { Logger, SimpleConsoleLogger, ConsoleLoggerWithPrefixes } from './logger';
import { ComponentA } from './ComponentA';
const App = () => {
return (
<DIProvider
// will update dependencies on each render
dependencies={[
dr => dr.registerImplementation(ConsoleLoggerWithPrefixes).as(Logger).inject('my message'),
]}
// will update dependencies on each render
self={[SimpleConsoleLogger]}
>
<ComponentA/>
</DIProvider>
);
};
// ComponentA.tsx
import {
use,
// if you have concerns about `use` name you can use `useDi` instead of
useDi, // alias for `use` hook
} from 'cheap-di-react';
import { Logger, SimpleConsoleLogger } from './logger';
const ComponentA = () => {
const logger = use(Logger); // will get ConsoleLoggerWithPrefixes instance here
logger.debug('foo'); // "my message: foo"
const simpleLogger = use(SimpleConsoleLogger); // will get SimpleConsoleLogger instance here, because it is registered as itself
simpleLogger.debug('bar'); // "bar"
return <>...</>;
};
You should memoized dependencies registration to avoid extra re-renders of entire tree
// App.tsx
import {
DIProvider,
use,
Dependency,
SelfDependency,
} from 'cheap-di-react';
import { ComponentA } from './ComponentA';
abstract class Foo {}
class FooImpl extends Foo {}
class Bar {}
const App = () => {
const dependencies = useMemo<Dependency[]>(() => [
dr => dr.registerImplementation(Foo).as(FooImpl),
], []);
const selfDependencies = useMemo<SelfDependency[]>(() => [Bar], []);
return (
<DIProvider
dependencies={dependencies}
self={selfDependencies}
>
<ComponentA/>
</DIProvider>
);
};
Or you may use DIProviderMemo to minimize code above
// App.tsx
import { DIProviderMemo, use } from 'cheap-di-react';
import { ComponentA } from './ComponentA';
abstract class Foo {}
class FooImpl extends Foo {}
class Bar {}
const App = () => {
const dependencies = useMemo<Dependency[]>(() => [
dr => dr.registerImplementation(Foo).as(FooImpl),
], []);
const selfDependencies = useMemo<SelfDependency[]>(() => [Bar], []);
return (
<DIProviderMemo
dependencies={[
dr => dr.registerImplementation(Foo).as(FooImpl),
]}
self={[Bar]}
>
<ComponentA/>
</DIProviderMemo>
);
};
FAQs
Dependency injection based on cheap-di for using in React components with React Context
The npm package cheap-di-react receives a total of 2 weekly downloads. As such, cheap-di-react popularity was classified as not popular.
We found that cheap-di-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 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
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.