Last active
March 26, 2017 02:25
-
-
Save tocttou/7a8b324146e206c7e6fb16638afff9b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component, PropTypes } from "react"; | |
| import io from "socket.io-client"; | |
| class App extends Component { | |
| constructor(props, context) { | |
| super(props, context); | |
| this.state = { | |
| message: null | |
| }; | |
| this.socket = io("https://localhost:3001"); | |
| } | |
| getChildContext() { | |
| return { | |
| socket: this.socket | |
| }; | |
| } | |
| render() { | |
| return ( | |
| <div> | |
| {this.props.children} | |
| </div> | |
| ); | |
| } | |
| } | |
| App.propTypes = { | |
| children: PropTypes.object.isRequired | |
| }; | |
| App.childContextTypes = { | |
| socket: PropTypes.object.isRequired | |
| }; | |
| export default App; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component, PropTypes } from 'react'; | |
| class HomePageComponent extends Component { | |
| constructor(props, context) { | |
| super(props, context); | |
| this.state = { | |
| message: null | |
| }; | |
| this.socket = context.socket; | |
| } | |
| componentWillMount() { | |
| this.socket.on('welcome', (data) => { | |
| this.setState({ message: data.msg }) | |
| }); | |
| } | |
| render() { | |
| return ( | |
| <div> | |
| {this.state.message} | |
| </div> | |
| ); | |
| } | |
| } | |
| HomePageComponent.contextTypes = { | |
| socket: PropTypes.object.isRequired | |
| }; | |
| export default HomePageComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment