Skip to content

Instantly share code, notes, and snippets.

@petehunt
Forked from zpao/gist:5686416
Last active June 3, 2022 06:37
  • Star 21 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save petehunt/5687276 to your computer and use it in GitHub Desktop.
/** @jsx React.DOM */
var MyRootComponent = React.createClass({
getInitialState: function() {
return {perMinute: '-', perDay: '-'};
},
componentDidMount: function() {
var socket = io.connect(this.props.url);
socket.on('business.clickout', this.setState.bind(this));
},
render: function() {
return <MyComponent perMinute={this.state.perMinute} perDay={this.state.perDay} />;
}
});
var MyComponent = React.createClass({
render: function() {
var perMinute = this.props.perMinute;
var perDay = this.props.perDay;
return (
<div>
<h3>Clickouts</h3>
<p>last Minute: {perMinute}</p>
<p>today: {perDay}</p>
</div>
);
}
});
React.renderComponent(
<MyRootComponent url="http://localhost:3000" />,
document.getElementById('domid')
);
@REBELinBLUE
Copy link

This is the sort of thing I am trying to do myself, I figured I should connect on my root component, but how would you pass the socket down to children components so that they may listen on specific channels?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment