Skip to content

Commit 57de4ff

Browse files
committed
allow Subscriber render out of context as a noop
need to talk to @mjackson about tradeoffs here, I’m undecided, but to avoid breaking Router, we need this for now, then we can decide to keep it this way or not
1 parent 6ec7e88 commit 57de4ff

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

modules/Subscriber.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@ class Subscriber extends React.Component {
1616

1717
componentWillMount() {
1818
const { channel } = this.props
19-
const subscribe = this.context.broadcasts[channel]
20-
21-
invariant(
22-
typeof subscribe === 'function',
23-
'<Subscriber channel="%s"> must be rendered in the context of a <Broadcast channel="%s">',
24-
channel,
25-
channel
26-
)
27-
28-
this.unsubscribe = subscribe(value => {
29-
// This function will be called once immediately.
30-
this.setState({ value })
31-
})
19+
20+
if (this.context.broadcasts) {
21+
const subscribe = this.context.broadcasts[channel]
22+
23+
invariant(
24+
typeof subscribe === 'function',
25+
'<Subscriber channel="%s"> must be rendered in the context of a <Broadcast channel="%s">',
26+
channel,
27+
channel
28+
)
29+
30+
this.unsubscribe = subscribe(value => {
31+
// This function will be called once immediately.
32+
this.setState({ value })
33+
})
34+
}
3235
}
3336

3437
componentWillUnmount() {

0 commit comments

Comments
 (0)