當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript react.useEffect函數代碼示例

本文整理匯總了TypeScript中react.useEffect函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript useEffect函數的具體用法?TypeScript useEffect怎麽用?TypeScript useEffect使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了useEffect函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: useState

export const useQuery = (value: string): Output => {
  const [result, setResult] = useState<Result>([]);
  const [loading, setLoading] = useState(false);
  const [loaded, setLoaded] = useState(false);
  const debounce = useRef<any>(null);

  useEffect(() => {
    worker.load().then(() => {
      setLoaded(true);
    });
  }, []);

  useEffect(
    () => {
      if (!value) return;

      if (debounce.current) {
        clearTimeout(debounce.current);
      }

      setLoading(true);

      debounce.current = setTimeout(() => {
        worker.solve(value).then((data: any) => {
          setResult(data);
          setLoading(false);
        });
      }, 140);
    },
    [value],
  );

  return { loaded, loading, data: result };
};
開發者ID:kerlends,項目名稱:word-solver,代碼行數:34,代碼來源:useQuery.ts

示例2: useDragDropManager

export function useDragLayer<CollectedProps>(
	collect: (monitor: DragLayerMonitor) => CollectedProps,
): CollectedProps {
	const dragDropManager = useDragDropManager()
	const monitor = dragDropManager.getMonitor()
	const [collected, updateCollected] = useCollector(monitor, collect)

	useEffect(() => monitor.subscribeToOffsetChange(updateCollected))
	useEffect(() => monitor.subscribeToStateChange(updateCollected))
	return collected
}
開發者ID:gaearon,項目名稱:react-dnd,代碼行數:11,代碼來源:useDragLayer.ts

示例3: useContext

export function useStorage<T>(key: string, defaultValue: T) {
  const storage = useContext(StorageContext)
  const [value, set] = useState(() => load(storage, key, defaultValue))

  useEffect(() => {
    store(storage, key, value)
  }, [value])

  useEffect(() => {
    set(load(storage, key, defaultValue))
  }, [key, storage])

  return [value, set] as const
}
開發者ID:8th713,項目名稱:cockpit-for-pixiv,代碼行數:14,代碼來源:useStorage.ts

示例4: useIntersectionObserver

export function useIntersectionObserver(
  target: React.RefObject<HTMLElement>,
  root: React.RefObject<HTMLElement>,
  options: IntersectionObserverInit
) {
  const [isIntersecting, setIntersecting] = useState(false);

  useEffect(() => {
    const observer = new IntersectionObserver(
      ([entry]) => {
        if (entry.isIntersecting !== isIntersecting) {
          setIntersecting(entry.isIntersecting);
        }
      },
      {
        root: root.current,
        // rootMargin: '0px 0px 0px 0px',
        // threshold: 0,
        ...options,
      }
    );
    if (target.current) {
      observer.observe(target.current);
    }
    return () => {
      if (target.current) {
        observer.unobserve(target.current);
      }
    };
  }, []);

  return isIntersecting;
}
開發者ID:LWJGL,項目名稱:lwjgl3-www,代碼行數:33,代碼來源:useIntersectionObserver.ts

示例5: useField

export default function useField(name: string) {
  const {
    initialData,
    errors,
    scopePath,
    unregisterField,
    registerField
  } = useContext(FormContext);

  const fieldName = scopePath ? `${scopePath}.${name}` : name;

  useEffect(() => {
    return () => unregisterField(fieldName);
  }, []);

  const defaultValue = dot.pick(fieldName, initialData);
  const error = errors[fieldName];

  return {
    fieldName,
    registerField,
    defaultValue,
    error
  };
}
開發者ID:isacmoura,項目名稱:unform,代碼行數:25,代碼來源:useField.ts

示例6: usePageVisibility

export function usePageVisibility() {
  const [visible, setVisible] = useState(getVisibility());

  useEffect(() => {
    const handler = () => setVisible(getVisibility());
    let visibilityChange: string;

    if (typeof document.hidden !== 'undefined') {
      // Opera 12.10 and Firefox 18 and later support
      visibilityChange = 'visibilitychange';
    } else if (
      //@ts-ignore
      typeof document.msHidden !== 'undefined'
    ) {
      visibilityChange = 'msvisibilitychange';
    } else if (
      //@ts-ignore
      typeof document.webkitHidden !== 'undefined'
    ) {
      visibilityChange = 'webkitvisibilitychange';
    } else {
      return;
    }

    document.addEventListener(visibilityChange, handler);
    return () => document.removeEventListener(visibilityChange, handler);
  }, []);

  return visible;
}
開發者ID:LWJGL,項目名稱:lwjgl3-www,代碼行數:30,代碼來源:usePageVisibility.ts

示例7: useCanvas

export function useCanvas(
    width: number,
    height: number,
    sourceCanvasRef?: RefObject<HTMLCanvasElement>
): [
    HTMLCanvasElement | null,
    CanvasRenderingContext2D | null,
    (ctx: CanvasRenderingContext2D) => void
] {
    const [canvas, setCanvas] = useState<HTMLCanvasElement | null>(null);
    const [ctx, setCtx] = useState<CanvasRenderingContext2D | null>(null);

    useEffect(() => {
        const canvas = sourceCanvasRef
            ? sourceCanvasRef.current!
            : document.createElement('canvas');
        canvas.width = width;
        canvas.height = height;
        const ctx = canvas.getContext('2d')!;
        setCanvas(canvas);
        setCtx(ctx);
    }, []);

    return [canvas, ctx, setCtx];
}
開發者ID:alcarin-org,項目名稱:alcarin,代碼行數:25,代碼來源:CanvasUtils.ts

示例8: useEffect

export function useApiFetcher<T>(
  fetcher: () => Promise<T>,
  inputs: InputIdentityList,
): T | null {
  const [result, setResult] = useState<T | null>(null)

  useEffect(() => {
    let cancelled = false
    ;(async () => {
      try {
        const result = await fetcher()
        if (!cancelled) setResult(result)
      } catch (e) {
        if (cancelled) return

        if (e instanceof NotAuthedError) {
          authService.setLoginRedirectUrl(history.location.pathname)
          history.push('/login')
        } else {
          throw e
        }
      }
    })()

    return () => {
      cancelled = true
    }
  }, inputs)

  return result
}
開發者ID:blindern,項目名稱:intern,代碼行數:31,代碼來源:api.ts

示例9: useFixtureStateRef

// Make latest fixture state accessible in ref callback
function useFixtureStateRef(fixtureState: FixtureState) {
  const ref = React.useRef(fixtureState);
  React.useEffect(() => {
    ref.current = fixtureState;
  });
  return ref;
}
開發者ID:skidding,項目名稱:cosmos,代碼行數:8,代碼來源:useFixtureState.ts

示例10: useRedirect

export function useRedirect(history: History, redirectLocation?: Location) {
  useEffect(() => {
    if (redirectLocation) {
      history.replace(redirectLocation);
    }
  });
}
開發者ID:elastic,項目名稱:kibana,代碼行數:7,代碼來源:useRedirect.ts


注:本文中的react.useEffect函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。