import React, { Component } from 'react'; import PubSub from 'pubsub-js'; class App extends Component { state = { show: true }; render() { return (
{this.state.show && }
); } } class Toggler extends Component { render() { return (
); } } class Panel extends Component { state = { color: this.props.initialColor }; subscriptions = [ PubSub.subscribe('SET YELLOW', () => this.setState({ color: 'yellow' })), PubSub.subscribe('SET PINK', () => this.setState({ color: 'pink' })), PubSub.subscribe('TOGGLE', () => { console.log(`Toggling from ${this.state.color}.`); this.setState(({ color }) => ({ color: color === 'red' ? this.props.initialColor : 'red' })); }) ]; componentWillUnmount = () => this.subscriptions.forEach(PubSub.unsubscribe); render() { return (
); } } export default App;