Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/app-framework/hooks.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/* Gather together and export some common hooks for convenience. */67declare const window: any;89import {10useCallback,11useEffect,12useLayoutEffect,13useMemo,14useRef,15useState,16} from "react";17import { useSelector } from "react-redux";18import { useAsyncEffect } from "use-async-effect";1920import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";21import useCounter from "./counter-hook";22import useDelayedRender from "./delayed-render-hook";23import useIsMountedRef from "./is-mounted-hook";24import useToggle from "./toggle-hook";2526export {27useAsyncEffect,28useCallback,29useCounter,30useDelayedRender,31useEffect,32useFrameContext,33useIsMountedRef,34useLayoutEffect,35useMemo,36useRef,37useSelector,38useState,39useToggle,40};4142export function useForceUpdate() {43const counterRef = useRef<any>(0);44const [, setCounter] = useState<number>(0);45return () => {46setCounter((counterRef.current += 1));47};48}4950/* Delay rendering for a certain number of milliseconds.51Use it like this at the top of your component to make it52so nothing gets rendered until after the delay:5354const render = useDelayedRender(props.index);55// ...56// any other hooks57// ...58if (!render) {59return <></>;60}6162*/6364function getWindowDimensions() {65const { innerWidth: width, innerHeight: height } = window;66return { width, height };67}6869export function useWindowDimensions() {70const [windowDimensions, setWindowDimensions] = useState(71getWindowDimensions(),72);7374useEffect(() => {75function handleResize() {76setWindowDimensions(getWindowDimensions());77}7879window.addEventListener("resize", handleResize);80return () => window.removeEventListener("resize", handleResize);81}, []);8283return windowDimensions;84}8586// if val changes, it updates the reference to the previous value.87// With that, from the component itself, you always have access to the previous value.88// Watch out, initially it's certainly undefined!89export function usePrevious<T>(val: T): T | null {90const prevRef = useRef<T | null>(null);9192useEffect(() => {93prevRef.current = val;94}, [val]);9596return prevRef.current;97}9899100