Last active
May 22, 2018 13:22
-
-
Save scottdomes/7d5c60c548aafe8dd604c7dccb7d6fb8 to your computer and use it in GitHub Desktop.
docs-clone-final
This file contains hidden or 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, { 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