forked from benmvp/react-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
67 lines (57 loc) · 1.99 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React from 'react';
import isEqual from 'lodash/isEqual';
import 'whatwg-fetch';
import EmailList from '../components/EmailList';
import EmailView from '../components/EmailView';
import EmailForm from '../components/EmailForm';
export default class EmailApp extends React.Component {
static propTypes = {
pollInterval: React.PropTypes.number
}
static defaultProps = {
pollInterval: 2000
}
state = {
emails: []
}
componentDidMount() {
// Retrieve emails from server once we know DOM exists
this._getUpdateEmails();
// Set up long-polling to continuously get new data
this._pollId = setInterval(
() => this._getUpdateEmails(),
this.props.pollInterval
);
}
componentWillUnmount() {
// Need to remember to clearInterval when the component gets
// removed from the DOM, otherwise the interval will keep going
// forever and leak memory
clearInterval(this._pollId);
}
_getUpdateEmails() {
return fetch('http://localhost:9090/emails')
.then((res) => res.json())
.then((emails) => {
// Because `emails` is a different reference from `this.state.emails`,
// the component will unnecessarily re-render even though the contents
// are the same. The virtual DOM will prevent the actual DOM from updating
// but it still actually has to run its diffing algorithm. So instead
// making this quick check here, saves unnecessary extra work
if (!isEqual(emails, this.state.emails)) {
this.setState({emails});
}
})
.catch((ex) => console.error(ex));
}
render() {
let {emails} = this.state;
return (
<main>
<EmailList emails={emails} />
<EmailView />
<EmailForm />
</main>
);
}
}