1
1
import classnames from 'classnames' ;
2
2
import * as PropTypes from 'prop-types' ;
3
+ import raf from 'raf' ;
3
4
import Trigger from 'rc-trigger' ;
4
5
import * as React from 'react' ;
5
6
import * as ReactDOM from 'react-dom' ;
@@ -98,6 +99,7 @@ export default class SelectTrigger extends React.Component<
98
99
public saveTriggerRef : ( ref : any ) => void ;
99
100
public dropdownMenuRef : DropdownMenu | null = null ;
100
101
public triggerRef : any ;
102
+ public rafInstance : number | null = null ;
101
103
102
104
constructor ( props : Partial < ISelectTriggerProps > ) {
103
105
super ( props ) ;
@@ -118,11 +120,24 @@ export default class SelectTrigger extends React.Component<
118
120
this . setDropdownWidth ( ) ;
119
121
}
120
122
123
+ public componentWillUnmount ( ) {
124
+ this . cancelRafInstance ( ) ;
125
+ }
126
+
121
127
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 ) ;
126
141
}
127
142
} ;
128
143
0 commit comments