Skip to content

Commit d3c7346

Browse files
Emiya0306loadchange
authored andcommitted
minor, fix scroll unlimited loop with PureComponent
1 parent 09688fe commit d3c7346

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

libs/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
export { default as CollapseTransition } from './collapse';
77
export { default as Transition } from './transition';
88
export { default as Component } from './component';
9+
export { default as PureComponent } from './pureComponent';
910
export { default as PropTypes } from './props';
1011
export { default as View } from './view';
1112
export { default as Animate } from './animate';

libs/pureComponent/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import classnames from 'classnames';
4+
5+
export default class PureComponent extends React.PureComponent {
6+
classNames(...args) {
7+
return classnames(args);
8+
}
9+
10+
className(...args) {
11+
const { className } = this.props;
12+
return this.classNames.apply(this, args.concat([className]));
13+
}
14+
15+
style(args) {
16+
const { style } = this.props;
17+
return Object.assign({}, args, style)
18+
}
19+
}
20+
21+
PureComponent.propTypes = {
22+
className: PropTypes.string,
23+
style: PropTypes.object
24+
};

src/scrollbar/Scrollbar.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import React from 'react';
33
import ReactDOM from 'react-dom'
44

5-
import { PropTypes, Component } from '../../libs';
5+
import { PropTypes, PureComponent } from '../../libs';
66
import { addResizeListener, removeResizeListener } from '../../libs/utils/resize-event';
77

88
import { getScrollBarWidth } from './scrollbar-width';
99
import { Bar } from './Bar'
1010

11-
export class Scrollbar extends Component {
11+
export class Scrollbar extends PureComponent {
1212
constructor(props) {
1313
super(props);
1414

@@ -61,7 +61,7 @@ export class Scrollbar extends Component {
6161

6262
_update() {
6363
let heightPercentage, widthPercentage;
64-
const wrap = this.wrap;
64+
const { wrap, state } = this;
6565
if (!wrap) return;
6666

6767
heightPercentage = (wrap.clientHeight * 100 / wrap.scrollHeight);
@@ -70,7 +70,9 @@ export class Scrollbar extends Component {
7070
let sizeHeight = (heightPercentage < 100) ? (heightPercentage + '%') : '';
7171
let sizeWidth = (widthPercentage < 100) ? (widthPercentage + '%') : '';
7272

73-
this.setState({sizeHeight, sizeWidth})
73+
if (state.sizeHeight !== sizeHeight || state.sizeWidth !== sizeWidth) {
74+
this.setState({sizeHeight, sizeWidth})
75+
}
7476
}
7577

7678
render() {

0 commit comments

Comments
 (0)