diff --git a/components/anchor/Anchor.tsx b/components/anchor/Anchor.tsx index c1ce836ca4..c7dbda2b41 100644 --- a/components/anchor/Anchor.tsx +++ b/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 { diff --git a/components/back-top/index.tsx b/components/back-top/index.tsx index e518888441..fce43476c1 100755 --- a/components/back-top/index.tsx +++ b/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 { } 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 { } 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 { } 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() {