Skip to content

Commit 2c0fda5

Browse files
lottamuslevithomason
authored andcommitted
fix(Sticky|Visibility): add null check on window (Semantic-Org#1990)
* 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
1 parent e372781 commit 2c0fda5

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/behaviors/Visibility/Visibility.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getElementType,
88
getUnhandledProps,
99
META,
10+
isBrowser,
1011
} from '../../lib'
1112

1213
/**
@@ -141,7 +142,7 @@ export default class Visibility extends Component {
141142
}
142143

143144
static defaultProps = {
144-
context: window,
145+
context: isBrowser ? window : null,
145146
continuous: false,
146147
once: true,
147148
}
@@ -170,11 +171,15 @@ export default class Visibility extends Component {
170171
}
171172

172173
componentDidMount() {
174+
if (!isBrowser) return
175+
173176
const { context } = this.props
174177
context.addEventListener('scroll', this.handleScroll)
175178
}
176179

177180
componentWillUnmount() {
181+
if (!isBrowser) return
182+
178183
const { context } = this.props
179184
context.removeEventListener('scroll', this.handleScroll)
180185
}

src/modules/Sticky/Sticky.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getElementType,
88
getUnhandledProps,
99
META,
10+
isBrowser,
1011
} from '../../lib'
1112

1213
/**
@@ -74,7 +75,7 @@ export default class Sticky extends Component {
7475
static defaultProps = {
7576
bottomOffset: 0,
7677
offset: 0,
77-
scrollContext: window,
78+
scrollContext: isBrowser ? window : null,
7879
}
7980

8081
static _meta = {
@@ -87,12 +88,16 @@ export default class Sticky extends Component {
8788
}
8889

8990
componentDidMount() {
91+
if (!isBrowser) return
92+
9093
const { scrollContext } = this.props
9194
this.handleUpdate()
9295
scrollContext.addEventListener('scroll', this.handleUpdate)
9396
}
9497

9598
componentWillUnmount() {
99+
if (!isBrowser) return
100+
96101
const { scrollContext } = this.props
97102
scrollContext.removeEventListener('scroll', this.handleUpdate)
98103
}

0 commit comments

Comments
 (0)