Skip to content

Commit

Permalink
fix(appear): use copy of ref for valid ref value in useEffect cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
linbudu599 committed Jul 18, 2023
1 parent c89b112 commit 21b85f1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/appear/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import type * as React from 'react';
export interface AppearProps {
children: React.ReactElement;
/**
* 元素进入可视区域触发的事件
* Triggered when the element enters the visible area.
* @param {CustomEvent} e
* @returns {void}
*/
onAppear?: (e: CustomEvent) => void;
/**
* 元素离开可视区域触发的事件
* Triggered when the element leaves the visible area.
* @param {CustomEvent} e
* @returns {void}
*/
Expand Down
26 changes: 13 additions & 13 deletions packages/appear/src/weex/index.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import type { ForwardedRef } from 'react';
import { useEffect, useRef, forwardRef, cloneElement, Children } from 'react';
import type { AppearProps } from '../typings';

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

Check warning on line 6 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

React Hook "useRef" is called conditionally. React Hooks must be called in the exact same order in every component render

Check warning on line 6 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, windows-latest)

React Hook "useRef" is called conditionally. React Hooks must be called in the exact same order in every component render

Check warning on line 6 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

React Hook "useRef" is called conditionally. React Hooks must be called in the exact same order in every component render

Check warning on line 6 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, windows-latest)

React Hook "useRef" is called conditionally. React Hooks must be called in the exact same order in every component render
const { children, onAppear, onDisappear } = props;

useEffect(() => {
onAppear &&
typeof childrenRef === 'object' &&
childrenRef.current?.addEventListener('appear', (e: CustomEvent) => onAppear(e));
// Use copy of childrenRef to avoid ref value changed in cleanup phase.
const nodeRef = typeof childrenRef === 'object' ? childrenRef.current : null;

// Return early if onAppear callback not specified.
onAppear && nodeRef?.addEventListener('appear', (e: CustomEvent) => onAppear(e));

return () => {
onAppear &&
typeof childrenRef === 'object' &&
childrenRef.current?.removeEventListener('appear', (e: CustomEvent) => onAppear(e));
onAppear && nodeRef?.removeEventListener('appear', (e: CustomEvent) => onAppear(e));
};
}, []);

Check warning on line 19 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

React Hook useEffect has missing dependencies: 'childrenRef' and 'onAppear'. Either include them or remove the dependency array

Check warning on line 19 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, windows-latest)

React Hook useEffect has missing dependencies: 'childrenRef' and 'onAppear'. Either include them or remove the dependency array

Check warning on line 19 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

React Hook useEffect has missing dependencies: 'childrenRef' and 'onAppear'. Either include them or remove the dependency array

Check warning on line 19 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, windows-latest)

React Hook useEffect has missing dependencies: 'childrenRef' and 'onAppear'. Either include them or remove the dependency array

useEffect(() => {
onDisappear &&
typeof childrenRef === 'object' &&
childrenRef.current?.addEventListener('disappear', (e: CustomEvent) => onDisappear(e));
const nodeRef = typeof childrenRef === 'object' ? childrenRef.current : null;

onDisappear && nodeRef?.addEventListener('disappear', (e: CustomEvent) => onDisappear(e));

return () => {
onDisappear &&
typeof childrenRef === 'object' &&
childrenRef.current?.removeEventListener('disappear', (e: CustomEvent) => onDisappear(e));
onDisappear && nodeRef?.removeEventListener('disappear', (e: CustomEvent) => onDisappear(e));
};
}, []);

Check warning on line 29 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

React Hook useEffect has missing dependencies: 'childrenRef' and 'onDisappear'. Either include them or remove the dependency array

Check warning on line 29 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, windows-latest)

React Hook useEffect has missing dependencies: 'childrenRef' and 'onDisappear'. Either include them or remove the dependency array

Check warning on line 29 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

React Hook useEffect has missing dependencies: 'childrenRef' and 'onDisappear'. Either include them or remove the dependency array

Check warning on line 29 in packages/appear/src/weex/index.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, windows-latest)

React Hook useEffect has missing dependencies: 'childrenRef' and 'onDisappear'. Either include them or remove the dependency array

Expand Down

0 comments on commit 21b85f1

Please sign in to comment.