MCPcopy Create free account
hub / github.com/jamiebuilds/create-react-context / Consumer

Class Consumer

src/implementation.js:124–178  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

122 }
123
124 class Consumer extends Component<ConsumerProps<T>, ConsumerState<T>> {
125 static contextTypes = {
126 [contextProp]: PropTypes.object
127 };
128
129 observedBits: number;
130
131 state: ConsumerState<T> = {
132 value: this.getValue()
133 };
134
135 componentWillReceiveProps(nextProps) {
136 let { observedBits } = nextProps;
137 this.observedBits =
138 observedBits === undefined || observedBits === null
139 ? MAX_SIGNED_31_BIT_INT // Subscribe to all changes by default
140 : observedBits;
141 }
142
143 componentDidMount() {
144 if (this.context[contextProp]) {
145 this.context[contextProp].on(this.onUpdate);
146 }
147 let { observedBits } = this.props;
148 this.observedBits =
149 observedBits === undefined || observedBits === null
150 ? MAX_SIGNED_31_BIT_INT // Subscribe to all changes by default
151 : observedBits;
152 }
153
154 componentWillUnmount() {
155 if (this.context[contextProp]) {
156 this.context[contextProp].off(this.onUpdate);
157 }
158 }
159
160 getValue(): T {
161 if (this.context[contextProp]) {
162 return this.context[contextProp].get();
163 } else {
164 return defaultValue;
165 }
166 }
167
168 onUpdate = (newValue, changedBits: number) => {
169 const observedBits: number = this.observedBits | 0;
170 if ((observedBits & changedBits) !== 0) {
171 this.setState({ value: this.getValue() });
172 }
173 };
174
175 render() {
176 return onlyChild(this.props.children)(this.state.value);
177 }
178 }
179
180 return {
181 Provider,

Callers

nothing calls this directly

Calls 1

getValueMethod · 0.95

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…