MCPcopy Index your code
hub / github.com/ctrlplusb/react-injectables / Injectable

Function Injectable

src/Injectable.js:6–57  ·  view source on GitHub ↗
(VeinComponent)

Source from the content-addressed store, hash-verified

4let injectionIdIndex = 0;
5
6const Injectable = (VeinComponent) => {
7 injectionIdIndex++;
8 const injectionId = `injectionId_${injectionIdIndex}`;
9
10 class InjectableComponent extends Component {
11 static injectionId = injectionId;
12
13 static contextTypes = {
14 registerInjectable: PropTypes.func.isRequired,
15 removeInjectable: PropTypes.func.isRequired
16 };
17
18 state = {
19 injections: []
20 }
21
22 componentWillMount() {
23 this.context.registerInjectable({
24 injectionId,
25 injectableInstance: this,
26 receive: (elements) => this.consume(elements)
27 });
28 }
29
30 componentWillUnmount() {
31 this.context.removeInjectable({
32 injectionId,
33 injectableInstance: this
34 });
35 }
36
37 consume = (elements) => {
38 if (elements.length !== this.state.injections.length ||
39 containsUniq(this.state.injections, elements)) {
40 this.setState({ injections: elements });
41 }
42 }
43
44 render() {
45 const keyed = keyedElements(`injections`, this.state.injections);
46
47 return (
48 <VeinComponent
49 injections={keyed}
50 {...this.props}
51 />
52 );
53 }
54 }
55
56 return InjectableComponent;
57};
58
59export default Injectable;

Callers 5

StatelessComponentFunction · 0.85
Injectable.test.jsFile · 0.85
Injector.test.jsFile · 0.85

Calls

no outgoing calls

Tested by 1

StatelessComponentFunction · 0.68