@@ -3,25 +3,42 @@ import firebase from 'APP/fire'
3
3
4
4
export default class extends React . Component {
5
5
componentDidMount ( ) {
6
- console . log ( this . props . fireRef )
6
+ // When the component mounts, start listening to the fireRef
7
+ // we were given.
7
8
this . listenTo ( this . props . fireRef )
8
9
}
9
10
10
11
componentWillUnmount ( ) {
12
+ // When we unmount, stop listening.
11
13
this . unsubscribe ( )
12
14
}
13
15
14
16
componentWillReceiveProps ( incoming , outgoing ) {
17
+ // When the props sent to us by our parent component change,
18
+ // start listening to the new firebase reference.
15
19
this . listenTo ( incoming . fireRef )
16
20
}
17
21
18
22
listenTo ( fireRef ) {
23
+ // If we're already listening to a ref, stop listening there.
19
24
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.
21
31
this . unsubscribe = ( ) => fireRef . off ( 'value' , listener )
22
32
}
23
33
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 &&
25
42
this . props . fireRef . set ( event . target . value )
26
43
27
44
render ( ) {
0 commit comments