Skip to content

Commit

Permalink
fix(Sticky|Visibility): add null check on window (#1990)
Browse files Browse the repository at this point in the history
* fix(Sticky|Visibility): add null check on window

fix(Sticky|Visibility): add null check on window for server side rendering

* initialize with null instead of empty object

* initialize with null instead of empty object

* Break out of lifecycle methods if not browser
  • Loading branch information
lottamus authored and levithomason committed Aug 28, 2017
1 parent e372781 commit 2c0fda5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/behaviors/Visibility/Visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getElementType,
getUnhandledProps,
META,
isBrowser,
} from '../../lib'

/**
Expand Down Expand Up @@ -141,7 +142,7 @@ export default class Visibility extends Component {
}

static defaultProps = {
context: window,
context: isBrowser ? window : null,
continuous: false,
once: true,
}
Expand Down Expand Up @@ -170,11 +171,15 @@ export default class Visibility extends Component {
}

componentDidMount() {
if (!isBrowser) return

const { context } = this.props
context.addEventListener('scroll', this.handleScroll)
}

componentWillUnmount() {
if (!isBrowser) return

const { context } = this.props
context.removeEventListener('scroll', this.handleScroll)
}
Expand Down
7 changes: 6 additions & 1 deletion src/modules/Sticky/Sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getElementType,
getUnhandledProps,
META,
isBrowser,
} from '../../lib'

/**
Expand Down Expand Up @@ -74,7 +75,7 @@ export default class Sticky extends Component {
static defaultProps = {
bottomOffset: 0,
offset: 0,
scrollContext: window,
scrollContext: isBrowser ? window : null,
}

static _meta = {
Expand All @@ -87,12 +88,16 @@ export default class Sticky extends Component {
}

componentDidMount() {
if (!isBrowser) return

const { scrollContext } = this.props
this.handleUpdate()
scrollContext.addEventListener('scroll', this.handleUpdate)
}

componentWillUnmount() {
if (!isBrowser) return

const { scrollContext } = this.props
scrollContext.removeEventListener('scroll', this.handleUpdate)
}
Expand Down

0 comments on commit 2c0fda5

Please sign in to comment.