Browse Source

fix: getDefaultTarget only be called in browser, close: #6926

pull/6935/head
Benjy Cui 7 years ago
parent
commit
2ba3a84331
  1. 2
      components/anchor/Anchor.tsx
  2. 12
      components/back-top/index.tsx

2
components/anchor/Anchor.tsx

@ -9,7 +9,7 @@ import getScroll from '../_util/getScroll';
import getRequestAnimationFrame from '../_util/getRequestAnimationFrame';
function getDefaultTarget() {
return typeof window !== 'undefined' ? window : null;
return window;
}
function getOffsetTop(element: HTMLElement): number {

12
components/back-top/index.tsx

@ -22,8 +22,7 @@ const easeInOutCubic = (t: number, b: number, c: number, d: number) => {
function noop() { }
function getDefaultTarget() {
return typeof window !== 'undefined' ?
window : null;
return window;
}
export interface BackTopProps {
@ -50,7 +49,8 @@ export default class BackTop extends React.Component<BackTopProps, any> {
}
getCurrentScrollTop = () => {
const targetNode = (this.props.target || getDefaultTarget)();
const getTarget = this.props.target || getDefaultTarget;
const targetNode = getTarget();
if (targetNode === window) {
return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
}
@ -73,7 +73,8 @@ export default class BackTop extends React.Component<BackTopProps, any> {
}
setScrollTop(value: number) {
const targetNode = (this.props.target || getDefaultTarget)();
const getTarget = this.props.target || getDefaultTarget;
const targetNode = getTarget();
if (targetNode === window) {
document.body.scrollTop = value;
document.documentElement.scrollTop = value;
@ -91,8 +92,9 @@ export default class BackTop extends React.Component<BackTopProps, any> {
}
componentDidMount() {
const getTarget = this.props.target || getDefaultTarget;
this.scrollEvent = addEventListener(getTarget(), 'scroll', this.handleScroll);
this.handleScroll();
this.scrollEvent = addEventListener((this.props.target || getDefaultTarget)(), 'scroll', this.handleScroll);
}
componentWillUnmount() {

Loading…
Cancel
Save