Skip to content

Returns a ref with a current value of true when a component is mounted.

License

Notifications You must be signed in to change notification settings

kaliberjs/use-is-mounted-ref

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

useIsMountedRef

Returns a ref with a current value of true when a component is mounted.

Motivation

Useful when working with server side rendering. Comparable to componentDidMount in a class-based component. Does not trigger a rerender.

Another use-case is to check if a component is still mounted after calling an async function.

Installation

yarn add @kaliber/use-is-mounted-ref

Usage

import { useIsMountedRef } from '@kaliber/use-is-mounted-ref'

function Component() {
  const [state, setState] = React.useState('⬇️')
  const isMountedRef = useIsMountedRef()

  // Use it to check if a component is still mounted after calling an async function.
  React.useEffect(
    () => {
      async function doSomethingAync() {
        try {
          await someAsyncFunction()
        } catch (err) {
          console.error(err)
          if (isMountedRef.current) setState('⚠️')
        }
        if (isMountedRef.current) setState('⬆️')
      }
      doSomethingAync()
    },
    [isMountedRef]
  )

  return (
    <>
      <div style={{ fontSize: '5em' }}>{state}</div>
    </>
  )
}

function someAsyncFunction() {
  return new Promise(resolve => { window.setTimeout(resolve, 1000) })
}

Disclaimer

This library is intended for internal use, we provide no support, use at your own risk. It does not import React, but expects it to be provided, which @kaliber/build can handle for you.

This library is not transpiled.

About

Returns a ref with a current value of true when a component is mounted.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •