Skip to content

Commit 424559f

Browse files
committed
add React 16.3.x support
1 parent 523a72c commit 424559f

File tree

4 files changed

+37
-67
lines changed

4 files changed

+37
-67
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## master (unreleased)
22

3+
## 4.3.0
4+
5+
- Add support for React 16.3.x new API
6+
- Prevent warnning on React 16.3.x deprecated `componentWillReceiveProps`
7+
38
## 4.2.1
49

510
- Prevent error when clicking a link to an anchor that does not exist. Thanks to @SBRK contribution

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-scrollchor",
3-
"version": "4.2.1",
3+
"version": "4.3.0",
44
"description": "A React component for scroll to #hash links with smooth animations",
55
"files": [
66
"lib"
@@ -47,8 +47,8 @@
4747
"babel-preset-stage-3": "6.x.x",
4848
"fbjs": "*",
4949
"mkdirp": "*",
50-
"prop-types": "^15.5.8",
51-
"react": "^15.5.4",
50+
"prop-types": "15.x.x",
51+
"react": "16.x.x",
5252
"rimraf": "2.x.x",
5353
"semistandard": "*",
5454
"snazzy": "7.x.x"

src/scrollchor.jsx

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { animateScroll, updateHistory } from './utils';
3+
import { animateScroll, updateHistory } from './helpers';
44

55
export default class Scrollchor extends React.Component {
66
constructor (props) {
77
super(props);
8-
this._setup(props);
8+
this.state = Scrollchor._stateHelper(props);
99
this.simulateClick = this._handleClick;
10+
isReact16_3() && delete Scrollchor.prototype.componentWillReceiveProps;
1011
}
1112

1213
static propTypes = {
@@ -21,34 +22,39 @@ export default class Scrollchor extends React.Component {
2122
disableHistory: PropTypes.bool
2223
}
2324

24-
_setup = props => {
25-
this._to = (props.to && props.to.replace(/^#/, '')) || '';
25+
static _stateHelper (props) {
2626
const {
2727
// default animate object
2828
offset = 0,
2929
duration = 400,
3030
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+
};
3739
}
3840

39-
_handleClick = event => {
40-
this._beforeAnimate(event);
41+
_handleClick = (event) => {
42+
this.state.beforeAnimate(event);
4143
event && event.preventDefault();
42-
const id = animateScroll(this._to, this._animate);
44+
const id = animateScroll(this.state.to, this.state.animate);
4345

4446
if (id) {
45-
this._disableHistory || updateHistory(id);
46-
this._afterAnimate(event);
47+
this.state.disableHistory || updateHistory(id);
48+
this.state.afterAnimate(event);
4749
}
4850
}
4951

52+
static getDerivedStateFromProps (props) {
53+
return Scrollchor._stateHelper(props);
54+
}
55+
5056
componentWillReceiveProps (props) {
51-
this._setup(props);
57+
this.setState(Scrollchor._stateHelper(props));
5258
}
5359

5460
render () {
@@ -65,3 +71,10 @@ export default class Scrollchor extends React.Component {
6571
function easeOutQuad (x, t, b, c, d) {
6672
return -c * (t /= d) * (t - 2) + b;
6773
}
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+))(?:-([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?(?:\+([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?$/; // eslint-disable-line no-useless-escape
78+
const [,, major, minor] = reSemver.exec(React.version);
79+
return major >= 16 && minor >= 3;
80+
}

src/utils.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)