Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit fd15cfd

Browse files
committed
Call ReactPerf.stop on componentWillUnmount
1 parent 2cff6b2 commit fd15cfd

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

src/__tests__/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,24 @@ describe('perf higher-order component', () => {
4242
expect(ReactPerf.start).toHaveBeenCalled();
4343
});
4444

45-
it('collects results and starts again in update', () => {
45+
it('stops ReactPerf on unmount', () => {
46+
const MyComp = () => <div className="my-comp" />;
47+
const MyCompPerf = perf(MyComp);
48+
49+
const result = mount(<MyCompPerf />);
50+
result.unmount();
51+
52+
expect(ReactPerf.stop).toHaveBeenCalled();
53+
});
54+
55+
it('stops, collects results and starts again in update', () => {
4656
const MyComp = () => <div className="my-comp" />;
4757
const MyCompPerf = perf(MyComp);
4858

4959
const result = mount(<MyCompPerf />);
5060
result.setProps();
5161

62+
expect(ReactPerf.stop).toHaveBeenCalled();
5263
expect(ReactPerf.getLastMeasurements).toHaveBeenCalled();
5364
expect(ReactPerf.printWasted).toHaveBeenCalled();
5465
expect(ReactPerf.start).toHaveBeenCalled();

src/index.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
22

33
/**
44
* Wraps the passed in `Component` in a higher-order component. It can then
@@ -8,25 +8,36 @@ import React from 'react';
88
* @param {ReactComponent} Component the component to wrap
99
* @return {ReactComponent} the wrapped component
1010
*/
11-
export default function perf(Component) {
12-
if (process.env.NODE_ENV !== 'production') {
13-
// eslint-disable-next-line global-require
14-
const ReactPerf = require('react-addons-perf');
15-
16-
return class Perf extends React.Component {
17-
componentDidMount() {
18-
ReactPerf.start();
19-
}
20-
componentDidUpdate() {
21-
const measurements = ReactPerf.getLastMeasurements();
22-
23-
ReactPerf.printWasted(measurements);
24-
ReactPerf.start();
25-
}
26-
render() {
27-
return <Component {...this.props} />;
28-
}
29-
};
11+
export default function perf(Target) {
12+
if (process.env.NODE_ENV === 'production') {
13+
return Target;
3014
}
31-
return Component;
15+
16+
// eslint-disable-next-line global-require
17+
const ReactPerf = require('react-addons-perf');
18+
19+
class Perf extends Component {
20+
componentDidMount() {
21+
ReactPerf.start();
22+
}
23+
24+
componentDidUpdate() {
25+
ReactPerf.stop();
26+
27+
const measurements = ReactPerf.getLastMeasurements();
28+
29+
ReactPerf.printWasted(measurements);
30+
ReactPerf.start();
31+
}
32+
33+
componentWillUnmount() {
34+
ReactPerf.stop();
35+
}
36+
37+
render() {
38+
return <Target {...this.props} />;
39+
}
40+
}
41+
42+
return Perf;
3243
}

0 commit comments

Comments
 (0)