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

This commit is contained in:
Benjy Cui 2017-07-24 09:58:56 +08:00
parent 547745271e
commit 2ba3a84331
2 changed files with 8 additions and 6 deletions

View File

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

View File

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