Skip to content

Commit cf2b592

Browse files
committed
Prevent unnecessary re-render from long polling
Performs an `isEqual` on data from GET with current emails state
1 parent 3f6b2b8 commit cf2b592

File tree

7 files changed

+56
-6
lines changed

7 files changed

+56
-6
lines changed

04-polling/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ Go to [Step 5 - Email View](https://github.com/benmvp/react-workshop/tree/master
1616
## Resources
1717

1818
- [Lifecycle methods](https://facebook.github.io/react/docs/component-specs.html#lifecycle-methods)
19+
- [lodash Documentation](https://lodash.com/docs)

04-polling/src/containers/App.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import isEqual from 'lodash/isEqual';
23
import 'whatwg-fetch';
34

45
import EmailList from '../components/EmailList';
@@ -39,7 +40,16 @@ export default class EmailApp extends React.Component {
3940
_getUpdateEmails() {
4041
return fetch('http://localhost:9090/emails')
4142
.then((res) => res.json())
42-
.then((emails) => this.setState({emails}))
43+
.then((emails) => {
44+
// Because `emails` is a different reference from `this.state.emails`,
45+
// the component will unnecessarily re-render even though the contents
46+
// are the same. The virtual DOM will prevent the actual DOM from updating
47+
// but it still actually has to run its diffing algorithm. So instead
48+
// making this quick check here, saves unnecessary extra work
49+
if (!isEqual(emails, this.state.emails)) {
50+
this.setState({emails});
51+
}
52+
})
4353
.catch((ex) => console.error(ex));
4454
}
4555

05-email-view/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,3 @@ Go to [Step 6 - Email Form](https://github.com/benmvp/react-workshop/tree/master
7373
- [React Hot Loader Getting Started](http://gaearon.github.io/react-hot-loader/getstarted/)
7474
- [Event System](https://facebook.github.io/react/docs/events.html)
7575
- [Dangerously Set innerHTML](https://facebook.github.io/react/tips/dangerously-set-inner-html.html)
76-
- [lodash Documentation](https://lodash.com/docs)

05-email-view/src/containers/App.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import find from 'lodash/find';
3+
import isEqual from 'lodash/isEqual';
34
import 'whatwg-fetch';
45

56
import EmailList from '../components/EmailList';
@@ -41,7 +42,16 @@ export default class EmailApp extends React.Component {
4142
_getUpdateEmails() {
4243
return fetch('http://localhost:9090/emails')
4344
.then((res) => res.json())
44-
.then((emails) => this.setState({emails}))
45+
.then((emails) => {
46+
// Because `emails` is a different reference from `this.state.emails`,
47+
// the component will unnecessarily re-render even though the contents
48+
// are the same. The virtual DOM will prevent the actual DOM from updating
49+
// but it still actually has to run its diffing algorithm. So instead
50+
// making this quick check here, saves unnecessary extra work
51+
if (!isEqual(emails, this.state.emails)) {
52+
this.setState({emails});
53+
}
54+
})
4555
.catch((ex) => console.error(ex));
4656
}
4757

06-email-form/src/containers/App.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import find from 'lodash/find';
3+
import isEqual from 'lodash/isEqual';
34
import 'whatwg-fetch';
45

56
import EmailList from '../components/EmailList';
@@ -41,7 +42,16 @@ export default class EmailApp extends React.Component {
4142
_getUpdateEmails() {
4243
return fetch('http://localhost:9090/emails')
4344
.then((res) => res.json())
44-
.then((emails) => this.setState({emails}))
45+
.then((emails) => {
46+
// Because `emails` is a different reference from `this.state.emails`,
47+
// the component will unnecessarily re-render even though the contents
48+
// are the same. The virtual DOM will prevent the actual DOM from updating
49+
// but it still actually has to run its diffing algorithm. So instead
50+
// making this quick check here, saves unnecessary extra work
51+
if (!isEqual(emails, this.state.emails)) {
52+
this.setState({emails});
53+
}
54+
})
4555
.catch((ex) => console.error(ex));
4656
}
4757

07-submit-email-form/src/containers/App.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import find from 'lodash/find';
3+
import isEqual from 'lodash/isEqual';
34
import 'whatwg-fetch';
45

56
import EmailList from '../components/EmailList';
@@ -41,7 +42,16 @@ export default class EmailApp extends React.Component {
4142
_getUpdateEmails() {
4243
return fetch('http://localhost:9090/emails')
4344
.then((res) => res.json())
44-
.then((emails) => this.setState({emails}))
45+
.then((emails) => {
46+
// Because `emails` is a different reference from `this.state.emails`,
47+
// the component will unnecessarily re-render even though the contents
48+
// are the same. The virtual DOM will prevent the actual DOM from updating
49+
// but it still actually has to run its diffing algorithm. So instead
50+
// making this quick check here, saves unnecessary extra work
51+
if (!isEqual(emails, this.state.emails)) {
52+
this.setState({emails});
53+
}
54+
})
4555
.catch((ex) => console.error(ex));
4656
}
4757

final/src/containers/App.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import find from 'lodash/find';
3+
import isEqual from 'lodash/isEqual';
34

45
import {addEmail, deleteEmail, getEmails, markRead, markUnread} from '../action-reducers';
56

@@ -74,7 +75,16 @@ export default class App extends React.Component {
7475

7576
_getUpdateEmails() {
7677
return getEmails()
77-
.then((emails) => this.setState({emails}));
78+
.then((emails) => {
79+
// Because `emails` is a different reference from `this.state.emails`,
80+
// the component will unnecessarily re-render even though the contents
81+
// are the same. The virtual DOM will prevent the actual DOM from updating
82+
// but it still actually has to run its diffing algorithm. So instead
83+
// making this quick check here, saves unnecessary extra work
84+
if (!isEqual(emails, this.state.emails)) {
85+
this.setState({emails});
86+
}
87+
});
7888
}
7989

8090
_handleItemSelect(emailId) {

0 commit comments

Comments
 (0)