浏览代码

fix: add react v17 `useTransition` polyfill (#6618)

David Luzar 2 年之前
父节点
当前提交
13780f390a
共有 2 个文件被更改,包括 11 次插入1 次删除
  1. 2 1
      src/components/LibraryMenuSection.tsx
  2. 9 0
      src/hooks/useTransition.ts

+ 2 - 1
src/components/LibraryMenuSection.tsx

@@ -1,4 +1,4 @@
-import React, { useEffect, useMemo, useState, useTransition } from "react";
+import React, { useEffect, useMemo, useState } from "react";
 import { LibraryUnit } from "./LibraryUnit";
 import { LibraryItem } from "../types";
 import Stack from "./Stack";
@@ -6,6 +6,7 @@ import clsx from "clsx";
 import { ExcalidrawElement, NonDeleted } from "../element/types";
 import { useAtom } from "jotai";
 import { libraryItemSvgsCache } from "../hooks/useLibraryItemSvg";
+import { useTransition } from "../hooks/useTransition";
 
 const ITEMS_PER_ROW = 4;
 const ROWS_RENDERED_PER_BATCH = 6;

+ 9 - 0
src/hooks/useTransition.ts

@@ -0,0 +1,9 @@
+import React, { useCallback } from "react";
+
+/** noop polyfill for v17. Subset of API available */
+function useTransitionPolyfill() {
+  const startTransition = useCallback((callback: () => void) => callback(), []);
+  return [false, startTransition] as const;
+}
+
+export const useTransition = React.useTransition || useTransitionPolyfill;