Skip to content

Instantly share code, notes, and snippets.

@scottdomes
Last active May 22, 2018 13:22
Show Gist options
  • Save scottdomes/7d5c60c548aafe8dd604c7dccb7d6fb8 to your computer and use it in GitHub Desktop.
Save scottdomes/7d5c60c548aafe8dd604c7dccb7d6fb8 to your computer and use it in GitHub Desktop.
docs-clone-final
import React, { Component } from 'react'
import './App.css'
import ActionCable from 'actioncable'
class App extends Component {
state = { text: '' }
componentDidMount() {
window.fetch('http://localhost:3001/notes/1').then(data => {
data.json().then(res => {
this.setState({ text: res.text })
})
})
const cable = ActionCable.createConsumer('ws://localhost:3001/cable')
this.sub = cable.subscriptions.create('NotesChannel', {
received: this.handleReceiveNewText
})
}
handleReceiveNewText = ({ text }) => {
if (text !== this.state.text) {
this.setState({ text })
}
}
handleChange = e => {
this.setState({ text: e.target.value })
this.sub.send({ text: e.target.value, id: 1 })
}
render() {
return (
<textarea
value={this.state.text}
onChange={this.handleChange}
/>
)
}
}
export default App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment