Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
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 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 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 10 weekly downloads. As such, cheap-di-react popularity was classified as not popular.
We found that cheap-di-react demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.