Skip to content

Commit

Permalink
chore: fixup ref usage
Browse files Browse the repository at this point in the history
  • Loading branch information
linbudu599 committed Jul 26, 2023
1 parent 21b85f1 commit 99ff43d
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/appear/src/weex/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { useEffect, useRef, forwardRef, cloneElement, Children } from 'react';
import type { AppearProps } from '../typings';

const WeexAppear = forwardRef<any, AppearProps>((props, ref) => {
const childrenRef: ForwardedRef<HTMLDivElement> = ref ?? useRef<HTMLDivElement>(null);
const internalRef = useRef<HTMLDivElement>(null);
const childrenRef: ForwardedRef<HTMLDivElement> = ref ?? internalRef;

const { children, onAppear, onDisappear } = props;

useEffect(() => {
Expand All @@ -16,7 +18,7 @@ const WeexAppear = forwardRef<any, AppearProps>((props, ref) => {
return () => {
onAppear && nodeRef?.removeEventListener('appear', (e: CustomEvent) => onAppear(e));
};
}, []);
}, [childrenRef, onAppear]);

useEffect(() => {
const nodeRef = typeof childrenRef === 'object' ? childrenRef.current : null;
Expand All @@ -26,7 +28,7 @@ const WeexAppear = forwardRef<any, AppearProps>((props, ref) => {
return () => {
onDisappear && nodeRef?.removeEventListener('disappear', (e: CustomEvent) => onDisappear(e));
};
}, []);
}, [childrenRef, onDisappear]);

return cloneElement(Children.only(children), { ref: childrenRef });
});
Expand Down

0 comments on commit 99ff43d

Please sign in to comment.