MCPcopy Create free account
hub / github.com/ctrlplusb/react-injectables / Injector

Function Injector

src/Injector.js:13–74  ·  view source on GitHub ↗
({ into } = {})

Source from the content-addressed store, hash-verified

11let injectorIndex = 0;
12
13const Injector = ({ into } = {}) => {
14 invariant(
15 into &&
16 typeof into === `function` &&
17 into.injectionId &&
18 into.contextTypes &&
19 into.contextTypes.registerInjectable &&
20 into.contextTypes.removeInjectable,
21 // Error message
22 invalidTargetMsg);
23
24 return function WrapComponent(InjectionComponent) {
25 invariant(
26 InjectionComponent &&
27 typeof InjectionComponent === `function`,
28 invalidInjectMsg
29 );
30
31 injectorIndex++;
32 const injectorId = `injector_${injectorIndex}`;
33
34 class InjectorComponent extends Component {
35 static contextTypes = {
36 registerInjector: PropTypes.func.isRequired,
37 updateInjector: PropTypes.func.isRequired,
38 removeInjector: PropTypes.func.isRequired
39 };
40
41 componentWillMount() {
42 this.context.registerInjector({
43 injectionId: into.injectionId,
44 injectorId,
45 injectorInstance: this,
46 inject: () => <InjectionComponent {...this.props} />
47 });
48 }
49
50 componentWillUpdate(nextProps) {
51 this.context.updateInjector({
52 injectionId: into.injectionId,
53 injectorId,
54 injectorInstance: this,
55 inject: () => <InjectionComponent {...nextProps} />
56 });
57 }
58
59 componentWillUnmount() {
60 this.context.removeInjector({
61 injectionId: into.injectionId,
62 injectorId,
63 injectorInstance: this
64 });
65 }
66
67 render() {
68 return null;
69 }
70 }

Callers 5

Injector.test.jsFile · 0.85
actualFunction · 0.85

Calls

no outgoing calls

Tested by 2

actualFunction · 0.68