Last active
January 29, 2020 09:14
-
-
Save yan13to/bbe36fa32469c816d6a686dc893f541a to your computer and use it in GitHub Desktop.
Sample importing of actioncable to react component in rails
This file contains 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 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