Skip to content

Instantly share code, notes, and snippets.

@yan13to
Last active January 29, 2020 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yan13to/bbe36fa32469c816d6a686dc893f541a to your computer and use it in GitHub Desktop.
Save yan13to/bbe36fa32469c816d6a686dc893f541a to your computer and use it in GitHub Desktop.
Sample importing of actioncable to react component in rails
import React from 'react';
import consumer from '../channels/consumer';
class NotificationsCount extends React.Component {
constructor(props) {
super(props)
this.state = { count: this.props.count }
}
componentDidMount() {
consumer.subscriptions.create("NotificationsChannel", {
connected(data) {
console.log('connected', data)
},
received(data) {
//this.setState({count: data.count})
console.log('received', data)
}
})
}
render() {
return (
<span className="badge" style={{color: 'red'}}>
{this.state.count}
</span>
)
}
}
export default NotificationsCount;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment