Skip to content

Commit

Permalink
Fix #4505: ScrollTop not mounting (#4507)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Jun 13, 2023
1 parent e5cf0d6 commit 4f26f87
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
6 changes: 4 additions & 2 deletions components/lib/hooks/useEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useUnmountEffect } from './useUnmountEffect';
export const useEventListener = ({ target = 'document', type, listener, options, when = true }) => {
const targetRef = React.useRef(null);
const listenerRef = React.useRef(null);
const prevListener = usePrevious(listener);
const prevOptions = usePrevious(options);

const bind = (bindOptions = {}) => {
Expand Down Expand Up @@ -38,11 +39,12 @@ export const useEventListener = ({ target = 'document', type, listener, options,
}, [target, when]);

React.useEffect(() => {
if (listenerRef.current && (listenerRef.current !== listener || prevOptions !== options)) {
// to properly compare functions we can implicitly converting the function to it's body's text as a String
if (listenerRef.current && ('' + prevListener !== '' + listener || prevOptions !== options)) {
unbind();
when && bind();
}
}, [listener, options]);
}, [listener, options, when]);

useUnmountEffect(() => {
unbind();
Expand Down
10 changes: 5 additions & 5 deletions components/lib/scrolltop/ScrollTop.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { PrimeReactContext } from '../api/context';
import { CSSTransition } from '../csstransition/CSSTransition';
import { useEventListener, useMountEffect, useUnmountEffect } from '../hooks/Hooks';
import { useEventListener, useUnmountEffect } from '../hooks/Hooks';
import { ChevronUpIcon } from '../icons/chevronup';
import { Ripple } from '../ripple/Ripple';
import { DomHandler, IconUtils, ZIndexUtils, classNames, mergeProps } from '../utils/Utils';
Expand Down Expand Up @@ -34,8 +34,8 @@ export const ScrollTop = React.memo(
const [bindDocumentScrollListener] = useEventListener({
target: 'window',
type: 'scroll',
listener: () => {
checkVisibility(DomHandler.getWindowScrollTop());
listener: (event) => {
event && checkVisibility(DomHandler.getWindowScrollTop());
}
});

Expand Down Expand Up @@ -71,10 +71,10 @@ export const ScrollTop = React.memo(
getElement: () => elementRef.current
}));

useMountEffect(() => {
React.useEffect(() => {
if (props.target === 'window') bindDocumentScrollListener();
else if (props.target === 'parent') bindParentScrollListener();
});
}, []); // eslint-disable-line react-hooks/exhaustive-deps

useUnmountEffect(() => {
ZIndexUtils.clear(scrollElementRef.current);
Expand Down

0 comments on commit 4f26f87

Please sign in to comment.