1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
- import { animateScroll , updateHistory } from './utils ' ;
3
+ import { animateScroll , updateHistory } from './helpers ' ;
4
4
5
5
export default class Scrollchor extends React . Component {
6
6
constructor ( props ) {
7
7
super ( props ) ;
8
- this . _setup ( props ) ;
8
+ this . state = Scrollchor . _stateHelper ( props ) ;
9
9
this . simulateClick = this . _handleClick ;
10
+ isReact16_3 ( ) && delete Scrollchor . prototype . componentWillReceiveProps ;
10
11
}
11
12
12
13
static propTypes = {
@@ -21,34 +22,39 @@ export default class Scrollchor extends React.Component {
21
22
disableHistory : PropTypes . bool
22
23
}
23
24
24
- _setup = props => {
25
- this . _to = ( props . to && props . to . replace ( / ^ # / , '' ) ) || '' ;
25
+ static _stateHelper ( props ) {
26
26
const {
27
27
// default animate object
28
28
offset = 0 ,
29
29
duration = 400 ,
30
30
easing = easeOutQuad
31
- } =
32
- props . animate || { } ;
33
- this . _animate = { offset, duration, easing } ;
34
- this . _beforeAnimate = props . beforeAnimate || function ( ) { } ;
35
- this . _afterAnimate = props . afterAnimate || function ( ) { } ;
36
- this . _disableHistory = props . disableHistory ;
31
+ } = props . animate || { } ;
32
+ return {
33
+ to : ( props . to && props . to . replace ( / ^ # / , '' ) ) || '' ,
34
+ animate : { offset, duration, easing } ,
35
+ beforeAnimate : props . beforeAnimate || function ( ) { } ,
36
+ afterAnimate : props . afterAnimate || function ( ) { } ,
37
+ disableHistory : props . disableHistory
38
+ } ;
37
39
}
38
40
39
- _handleClick = event => {
40
- this . _beforeAnimate ( event ) ;
41
+ _handleClick = ( event ) => {
42
+ this . state . beforeAnimate ( event ) ;
41
43
event && event . preventDefault ( ) ;
42
- const id = animateScroll ( this . _to , this . _animate ) ;
44
+ const id = animateScroll ( this . state . to , this . state . animate ) ;
43
45
44
46
if ( id ) {
45
- this . _disableHistory || updateHistory ( id ) ;
46
- this . _afterAnimate ( event ) ;
47
+ this . state . disableHistory || updateHistory ( id ) ;
48
+ this . state . afterAnimate ( event ) ;
47
49
}
48
50
}
49
51
52
+ static getDerivedStateFromProps ( props ) {
53
+ return Scrollchor . _stateHelper ( props ) ;
54
+ }
55
+
50
56
componentWillReceiveProps ( props ) {
51
- this . _setup ( props ) ;
57
+ this . setState ( Scrollchor . _stateHelper ( props ) ) ;
52
58
}
53
59
54
60
render ( ) {
@@ -65,3 +71,10 @@ export default class Scrollchor extends React.Component {
65
71
function easeOutQuad ( x , t , b , c , d ) {
66
72
return - c * ( t /= d ) * ( t - 2 ) + b ;
67
73
}
74
+
75
+ // Check for React version 16.3.x and beyond
76
+ function isReact16_3 ( ) { // eslint-disable-line camelcase
77
+ const reSemver = / ^ v ? ( ( \d + ) \. ( \d + ) \. ( \d + ) ) (?: - ( [ \d A - Z a - z \- ] + (?: \. [ \d A - Z a - z \- ] + ) * ) ) ? (?: \+ ( [ \d A - Z a - z \- ] + (?: \. [ \d A - Z a - z \- ] + ) * ) ) ? $ / ; // eslint-disable-line no-useless-escape
78
+ const [ , , major , minor ] = reSemver . exec ( React . version ) ;
79
+ return major >= 16 && minor >= 3 ;
80
+ }
0 commit comments