Skip to content

rodw1995/use-mounted-state

Repository files navigation

use-mounted-state

React hook to check mounted state

Install

yarn add @rodw95/use-mounted-state

// or

npm install @rodw95/use-mounted-state

Example

import useIsMounted from '@rodw95/use-mounted-state';
import React, { useCallback, useState } from 'react';

export default () => {
  const isMounted = useIsMounted();
  const [state, setState] = useState(null);

  const doFetch = useCallback(async () => {
    const result = await fetchAsync();
    if (isMounted()) {
      // Only set state if still mounted
      setState(result);
    }
  });

  return (
    // ...
  );
};

License

MIT

About

React hook to check mounted state

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published