MCPcopy Create free account
hub / github.com/ReactTraining/react-broadcast / createContextEmitter

Function createContextEmitter

modules/index.js:3–49  ·  view source on GitHub ↗
(contextKey)

Source from the content-addressed store, hash-verified

1import React, { PropTypes } from 'react'
2
3const createContextEmitter = (contextKey) => (
4
5 class Emitter extends React.Component {
6
7 static propTypes = {
8 children: PropTypes.node,
9 value: PropTypes.any
10 }
11
12 static childContextTypes = {
13 [contextKey]: PropTypes.object
14 }
15
16 constructor() {
17 super()
18 this.subscribers = []
19 }
20
21 getChildContext() {
22 return {
23 [contextKey]: {
24 getInitialValue: () => {
25 return this.props.value
26 },
27
28 subscribe: (subscriber) => {
29 this.subscribers.push(subscriber)
30 return () => {
31 this.subscribers = this.subscribers.filter(
32 alleged => alleged !== subscriber
33 )
34 }
35 }
36 }
37 }
38 }
39
40 componentWillReceiveProps(nextProps) {
41 this.subscribers.forEach(f => f(nextProps.value))
42 }
43
44 render() {
45 return this.props.children
46 }
47
48 }
49)
50
51const createContextSubscriber = (contextKey) => (
52

Callers 2

coords.jsFile · 0.90
index-test.jsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…