Skip to content

Commit 539289e

Browse files
queervioletqueerviolet
queerviolet
authored and
queerviolet
committed
comments
1 parent f045ad4 commit 539289e

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

app/components/Scratchpad.jsx

+20-3
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,42 @@ import firebase from 'APP/fire'
33

44
export default class extends React.Component {
55
componentDidMount() {
6-
console.log(this.props.fireRef)
6+
// When the component mounts, start listening to the fireRef
7+
// we were given.
78
this.listenTo(this.props.fireRef)
89
}
910

1011
componentWillUnmount() {
12+
// When we unmount, stop listening.
1113
this.unsubscribe()
1214
}
1315

1416
componentWillReceiveProps(incoming, outgoing) {
17+
// When the props sent to us by our parent component change,
18+
// start listening to the new firebase reference.
1519
this.listenTo(incoming.fireRef)
1620
}
1721

1822
listenTo(fireRef) {
23+
// If we're already listening to a ref, stop listening there.
1924
if (this.unsubscribe) this.unsubscribe()
20-
const listener = fireRef.on('value', snapshot => this.setState({value: snapshot.val()}))
25+
26+
// Whenever our ref's value changes, set {value} on our state.
27+
const listener = fireRef.on('value', snapshot =>
28+
this.setState({value: snapshot.val()}))
29+
30+
// Set unsubscribe to be a function that detaches the listener.
2131
this.unsubscribe = () => fireRef.off('value', listener)
2232
}
2333

24-
write = (event) => this.props.fireRef &&
34+
// Write is defined using the class property syntax.
35+
// This is roughly equivalent to saying,
36+
//
37+
// this.write = event => (etc...)
38+
//
39+
// in the constructor. Incidentally, this means that write
40+
// is always bound to this.
41+
write = event => this.props.fireRef &&
2542
this.props.fireRef.set(event.target.value)
2643

2744
render() {

0 commit comments

Comments
 (0)