Skip to content

Commit

Permalink
fix: mode为visible为true时异常渲染的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
IVLIU committed Feb 28, 2024
1 parent 7c3bfdc commit 17208e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 3 additions & 3 deletions example/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'react-app-polyfill/ie11';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Offscreen } from '../.';
import { Activity } from '../.';

const Count = () => {
const [count, setCount] = React.useState(0);
Expand All @@ -17,9 +17,9 @@ const App = () => {
<button onClick={() => setVisible(!visible)}>{visible ? 'hidden' : 'show'}</button>
<button onClick={() => setDestroy(true)}>destroy</button>
{!destroy && (
<Offscreen mode={visible ? 'visible' : 'hidden'}>
<Activity mode={visible ? 'visible' : 'hidden'}>
<Count />
</Offscreen>
</Activity>
)}
</div>
);
Expand Down
19 changes: 13 additions & 6 deletions src/Repeater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@ export const Repeater: FC<IProps> = (props) => {
// props
const { mode, children } = props;
// refs
const resolveRef = useRef<() => void>();
const promiseRef = useRef<Promise<void> | null>(null);
const resolveRef = useRef<(() => void) | null>(null);
// methods
const resolvePromise = (ignoreMode?: boolean) => {
if (
(ignoreMode || mode === "visible") &&
typeof resolveRef.current === "function"
) {
resolveRef.current();
resolveRef.current = void 0;
resolveRef.current = null;
promiseRef.current = null;
}
};
// effect
useEffect(() => () => resolvePromise(true), []);

if (mode === "hidden" && typeof resolveRef.current === "undefined") {
const promise = new Promise<void>(
(resolve) => (resolveRef.current = resolve),
);
if (mode === "hidden") {
if(resolveRef.current === null) {
promiseRef.current = new Promise<void>(
(resolve) => (resolveRef.current = resolve),
);
}

const promise = promiseRef.current!;

if ("use" in React && typeof React.use === "function") {
(React.use as <T>(primise: Promise<T>) => T)(promise);
} else {
Expand Down

0 comments on commit 17208e3

Please sign in to comment.