Microhook

website·Indexed 30 days ago

https://microhook.netlify.app/

A collection of lightweight and composable React hooks for common UI tasks. It provides a set of npm packages including hooks such as useRestHeight, usePrefetch, usePortal, useOutClick, useIntersectionObserver, useLoading, and useMeasure, with built-in TypeScript support.

Tokens
4.8K
Snippets
21
Records
51
Agent score
25%

What's inside Microhook

  1. Overview of built-in React Hooks

    React hooks (introduced in version 16.8) allow the use of state and other React features in functional components without writing classes, facilitating the reuse of stateful logic.

    Commonly used built-in hooks include:

    • useState: Adds state to functional components; returns a stateful value and a setter function.
    • useEffect: Handles side effects (e.g., data fetching, event listeners), replacing class lifecycle methods.
    • useContext: Consumes React context to share data across the component tree without prop drilling.
    • useReducer: Manages complex state transitions via a reducer function.
    • useCallback: Memoizes a function to prevent unnecessary re-creations and re-renders unless dependencies change.
    • useMemo: Memoizes a calculated value to prevent expensive recalculations unless dependencies change.
    • useRef: Creates a mutable ref object that persists for the component's lifetime, typically used for DOM access.
    • useLayoutEffect: Similar to useEffect, but executes synchronously after all DOM mutations, useful for measuring DOM nodes.
  2. Overview of built-in React Hooks

    React hooks (introduced in version 16.8) allow the use of state and other React features in functional components without writing classes, facilitating the reuse of stateful logic between components.
  3. Overview of built-in React Hooks

    React hooks (introduced in version 16.8) allow functional components to use state and other React features without writing a class, facilitating the reuse of stateful logic between components.
  4. Overview of Microhook

    Microhook is a lightweight, fully typed React hooks library designed to simplify component state management and side-effect handling while minimizing bundle size. Key features include a small footprint (a few kilobytes), intuitive APIs, and full TypeScript support for improved type safety.
  5. Verify system requirements for microhook

    To use microhook, ensure your environment meets the following minimum version requirements:

    • React: version 16.8 or higher
    • Node.js: version 16.14 or above (verify by running node -v)

    It is recommended to use nvm for managing Node versions and to install all suggested dependencies during the Node.js installation process.

  6. Render a component to a different DOM location using usePortal

    usePortal is a custom React hook that renders a component to a specified DOM container other than its parent. This is typically used for modals, tooltips, or elements requiring specific DOM positioning.

    To use it, provide a callback function that returns the JSX to be rendered and a reference to the target container element (e.g., document.body). The callback should be wrapped in useCallback to prevent unnecessary re-renders.

    import { useCallback } from 'react';
    import { usePortal } from 'microhook';
    
    interface Props {
      text: string;
    }
    
    function MyComponent({ text }: Props) {
      const container = document.body;
      const render = useCallback(() => <div>{text}</div>, [text]);
    
      usePortal(render, container);
    
      return null;
    }