Skip to content

Commit dcb6397

Browse files
zzzJHafc163
authored andcommitted
性能调优,将获取offsetwidth的行为放置requestAnimationFrame中 (#395)
* chore: 性能调优,将获取offsetwidth的行为放置raf中 背景:针对表格大量数据下调优,表格下的分页控件使用了改控件 优化:将获取offsetwidth的行为放置raf中 * chore: 使用raf库替代window.raf * fix: 在每次raf前取消上次raf * feat: 新增@types/raf * chore: 库raf正确使用 * remove custom declare of 'raf' * chore: 重复代码抽离
1 parent 176d584 commit dcb6397

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"devDependencies": {
4848
"@types/classnames": "^2.2.6",
4949
"@types/enzyme": "^3.1.15",
50+
"@types/raf": "^3.4.0",
5051
"@types/react": "^16.7.17",
5152
"@types/react-dom": "^16.0.11",
5253
"@types/warning": "^3.0.0",

src/SelectTrigger.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import classnames from 'classnames';
22
import * as PropTypes from 'prop-types';
3+
import raf from 'raf';
34
import Trigger from 'rc-trigger';
45
import * as React from 'react';
56
import * as ReactDOM from 'react-dom';
@@ -98,6 +99,7 @@ export default class SelectTrigger extends React.Component<
9899
public saveTriggerRef: (ref: any) => void;
99100
public dropdownMenuRef: DropdownMenu | null = null;
100101
public triggerRef: any;
102+
public rafInstance: number | null = null;
101103

102104
constructor(props: Partial<ISelectTriggerProps>) {
103105
super(props);
@@ -118,11 +120,24 @@ export default class SelectTrigger extends React.Component<
118120
this.setDropdownWidth();
119121
}
120122

123+
public componentWillUnmount() {
124+
this.cancelRafInstance();
125+
}
126+
121127
public setDropdownWidth = () => {
122-
const dom = ReactDOM.findDOMNode(this) as HTMLDivElement;
123-
const width = dom.offsetWidth;
124-
if (width !== this.state.dropdownWidth) {
125-
this.setState({ dropdownWidth: width });
128+
this.cancelRafInstance();
129+
this.rafInstance = raf(() => {
130+
const dom = ReactDOM.findDOMNode(this) as HTMLDivElement;
131+
const width = dom.offsetWidth;
132+
if (width !== this.state.dropdownWidth) {
133+
this.setState({ dropdownWidth: width });
134+
}
135+
});
136+
};
137+
138+
public cancelRafInstance = () => {
139+
if (this.rafInstance) {
140+
raf.cancel(this.rafInstance);
126141
}
127142
};
128143

typings/index.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@ declare module 'rc-util/lib/KeyCode';
1212

1313
declare module 'dom-scroll-into-view';
1414

15-
declare module 'raf';
16-
1715
declare module 'rc-trigger';

0 commit comments

Comments
 (0)