useMounted

    API

    import useMounted from '@restart/hooks/useMounted'
    • useMounted() => () => boolean

      Track whether a component is current mounted. Generally less preferable than properlly canceling effects so they don't run after a component is unmounted, but helpful in cases where that isn't feasible, such as a Promise resolution.

      Return Value () => boolean

      a function that returns the current isMounted state of the component

      const [data, setData] = useState(null)
      const isMounted = useMounted()
      useEffect(() => {
      fetchdata().then((newData) => {
      if (isMounted()) {
      setData(newData);
      }
      })
      })