Skip to content

Instantly share code, notes, and snippets.

@tocttou
Last active March 26, 2017 02:25
Show Gist options
  • Select an option

  • Save tocttou/7a8b324146e206c7e6fb16638afff9b0 to your computer and use it in GitHub Desktop.

Select an option

Save tocttou/7a8b324146e206c7e6fb16638afff9b0 to your computer and use it in GitHub Desktop.
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;
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