Completed
Pull Request — master (#41)
by Alejandro
03:20 queued 37s
created

src/common/ScrollToTop.js   A

Complexity

Total Complexity 4
Complexity/F 2

Size

Lines of Code 35
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 15.38%

Importance

Changes 0
Metric Value
cc 0
wmc 4
eloc 21
c 0
b 0
f 0
nc 1
mnd 1
bc 3
fnc 2
dl 0
loc 35
rs 10
bpm 1.5
cpm 2
noi 0
ccs 2
cts 13
cp 0.1538
crap 0
1
import React from 'react';
2
import { withRouter } from 'react-router-dom';
3 4
import PropTypes from 'prop-types';
4
5
export class ScrollToTopComponent extends React.Component {
6
  static propTypes = {
7
    location: PropTypes.object,
8
    window: PropTypes.shape({
9
      scrollTo: PropTypes.func,
10
    }),
11
    children: PropTypes.node,
12
  };
13
  static defaultProps = {
14
    window,
15
  };
16
17
  componentDidUpdate(prevProps) {
18
    const { location, window } = this.props;
19
20 2
    if (location !== prevProps.location) {
21
      window.scrollTo(0, 0);
22
    }
23
  }
24
25
  render() {
26
    return this.props.children;
27
  }
28
}
29
30
const ScrollToTop = withRouter(ScrollToTopComponent);
31
32
export default ScrollToTop;
33