Convert Figma logo to code with AI

uidotdev logousehooks

A collection of modern, server-safe React hooks – from the ui.dev team

9,220
496
9,220
44

Top Related Projects

41,502

React Hooks β€” πŸ‘

Awesome React Hooks

13,877

A high-quality & reliable React Hooks library. https://ahooks.pages.dev/

1,889

React hooks done right, for browser and SSR.

πŸ”₯ A collection of beautiful and (hopefully) useful React hooks to speed-up your components and hooks development πŸ”₯

A collection of useful React hooks

Quick Overview

usehooks is a collection of modern, server-side rendered (SSR) React Hooks. It provides a set of reusable custom hooks that can be easily integrated into React applications, enhancing functionality and simplifying common tasks. The project aims to offer well-documented, tested, and production-ready hooks for developers.

Pros

  • Wide variety of hooks covering common use cases
  • Server-side rendering support for improved performance and SEO
  • Well-documented with examples and TypeScript definitions
  • Regularly updated and maintained

Cons

  • Some hooks may have dependencies that increase bundle size
  • Learning curve for developers new to React Hooks
  • Potential for over-reliance on external hooks instead of custom solutions

Code Examples

  1. Using the useLocalStorage hook:
import { useLocalStorage } from "@uidotdev/usehooks";

function App() {
  const [name, setName] = useLocalStorage("name", "");
  return (
    <input
      type="text"
      placeholder="Enter your name"
      value={name}
      onChange={(e) => setName(e.target.value)}
    />
  );
}

This example demonstrates how to use the useLocalStorage hook to persist and retrieve data from local storage.

  1. Implementing infinite scroll with useInfiniteScroll:
import { useInfiniteScroll } from "@uidotdev/usehooks";

function App() {
  const [items, setItems] = useState([]);
  const [isFetching, fetchMoreItems] = useInfiniteScroll(fetchItems);

  async function fetchItems() {
    const newItems = await getMoreItems();
    setItems([...items, ...newItems]);
  }

  return (
    <div>
      {items.map((item) => (
        <div key={item.id}>{item.name}</div>
      ))}
      {isFetching && <div>Loading more...</div>}
    </div>
  );
}

This example shows how to implement infinite scrolling using the useInfiniteScroll hook.

  1. Managing form state with useForm:
import { useForm } from "@uidotdev/usehooks";

function App() {
  const { values, errors, touched, handleChange, handleSubmit } = useForm({
    initialValues: { email: "", password: "" },
    onSubmit: (values) => {
      console.log(values);
    },
    validate: (values) => {
      const errors = {};
      if (!values.email) errors.email = "Required";
      if (!values.password) errors.password = "Required";
      return errors;
    },
  });

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="email"
        name="email"
        value={values.email}
        onChange={handleChange}
      />
      {touched.email && errors.email && <div>{errors.email}</div>}
      <input
        type="password"
        name="password"
        value={values.password}
        onChange={handleChange}
      />
      {touched.password && errors.password && <div>{errors.password}</div>}
      <button type="submit">Submit</button>
    </form>
  );
}

This example demonstrates how to use the useForm hook to manage form state, validation, and submission.

Getting Started

To start using usehooks in your React project:

  1. Install the package:

    npm install @uidotdev/usehooks
    
  2. Import and use the desired hooks in your components:

    import { useLocalStorage, useInfiniteScroll, useForm } from "@uidotdev/usehooks";
    
    function MyComponent() {
      // Use the hooks here
    }
    
  3. Refer to the documentation for specific usage instructions for each hook.

Competitor Comparisons

41,502

React Hooks β€” πŸ‘

Pros of react-use

  • Larger collection of hooks (100+) covering a wide range of use cases
  • More frequent updates and active maintenance
  • Includes TypeScript definitions out of the box

Cons of react-use

  • Larger bundle size due to the extensive collection of hooks
  • Some hooks may be overly specific or complex for simple use cases
  • Steeper learning curve due to the vast number of available hooks

Code Comparison

react-use:

import { useToggle, useList } from 'react-use';

const [isOn, toggleOn] = useToggle(false);
const [list, { push, filter }] = useList([1, 2, 3]);

usehooks:

import { useToggle, useArray } from 'usehooks';

const [isOn, setIsOn] = useToggle(false);
const { array, set, push, remove, filter } = useArray([1, 2, 3]);

Both libraries offer similar functionality, but react-use tends to provide more specialized hooks, while usehooks focuses on simpler, more general-purpose hooks. The code structure and naming conventions are slightly different, but the overall usage is comparable. react-use's extensive collection may be beneficial for complex projects, while usehooks might be more suitable for smaller applications or developers who prefer a more curated set of hooks.

Awesome React Hooks

Pros of awesome-react-hooks

  • Extensive collection of React hooks from various sources
  • Community-driven with contributions from multiple developers
  • Categorized list for easy navigation and discovery

Cons of awesome-react-hooks

  • Less curated, may include outdated or lower-quality hooks
  • No standardized documentation or usage examples
  • Requires more effort to evaluate and integrate hooks

Code Comparison

awesome-react-hooks (example from a linked repository):

const useWindowSize = () => {
  const [size, setSize] = useState([window.innerWidth, window.innerHeight]);
  useEffect(() => {
    const handleResize = () => setSize([window.innerWidth, window.innerHeight]);
    window.addEventListener('resize', handleResize);
    return () => window.removeEventListener('resize', handleResize);
  }, []);
  return size;
};

usehooks (example from the repository):

export function useWindowSize() {
  const [size, setSize] = useState({
    width: window.innerWidth,
    height: window.innerHeight,
  });
  useEffect(() => {
    const handler = () => setSize({
      width: window.innerWidth,
      height: window.innerHeight,
    });
    window.addEventListener('resize', handler);
    return () => window.removeEventListener('resize', handler);
  }, []);
  return size;
}

Both repositories offer valuable resources for React developers. awesome-react-hooks provides a comprehensive list of hooks from various sources, while usehooks offers a more curated selection with consistent documentation and examples. The choice between them depends on whether you prefer a wide variety of options or a more streamlined, quality-focused collection.

13,877

A high-quality & reliable React Hooks library. https://ahooks.pages.dev/

Pros of hooks

  • More comprehensive collection of hooks (50+)
  • Better documentation with live demos and API references
  • Supports TypeScript out of the box

Cons of hooks

  • Larger bundle size due to more hooks
  • Steeper learning curve with more complex hooks
  • Less focused on simplicity compared to usehooks

Code Comparison

usehooks example (useLocalStorage):

const [value, setValue] = useLocalStorage("key", "initial");

hooks example (useLocalStorageState):

const [message, setMessage] = useLocalStorageState(
  'use-local-storage-state-demo1',
  {
    defaultValue: 'Hello~',
  }
);

Both repositories provide custom React hooks, but hooks offers a more extensive collection with better documentation. usehooks focuses on simplicity and ease of use, while hooks provides more advanced functionality at the cost of increased complexity.

usehooks is ideal for developers looking for quick, simple solutions, while hooks is better suited for larger projects requiring more diverse and powerful hooks. The choice between the two depends on the specific needs of your project and your familiarity with React hooks.

1,889

React hooks done right, for browser and SSR.

Pros of react-hookz/web

  • Larger collection of hooks (50+) compared to usehooks (10+)
  • More frequent updates and active maintenance
  • Comprehensive documentation with examples and TypeScript support

Cons of react-hookz/web

  • Steeper learning curve due to the larger number of hooks
  • Less focus on simplicity and ease of use for beginners
  • May include hooks that are not commonly used in everyday development

Code Comparison

usehooks:

import { useLocalStorage } from 'usehooks'

const [name, setName] = useLocalStorage('name', 'Bob')

react-hookz/web:

import { useLocalStorageValue } from '@react-hookz/web'

const [name, setName] = useLocalStorageValue('name', {
  defaultValue: 'Bob'
})

Both libraries provide similar functionality for local storage hooks, but react-hookz/web offers more configuration options and a slightly different API structure.

Overall, usehooks is more suitable for beginners and simple projects, while react-hookz/web offers a wider range of hooks and more advanced features for complex applications. The choice between the two depends on the project requirements and the developer's experience level with React hooks.

πŸ”₯ A collection of beautiful and (hopefully) useful React hooks to speed-up your components and hooks development πŸ”₯

Pros of beautiful-react-hooks

  • More extensive collection of hooks (30+) compared to usehooks
  • Includes TypeScript definitions out of the box
  • Provides a playground for testing hooks directly in the browser

Cons of beautiful-react-hooks

  • Less frequently updated than usehooks
  • Documentation is not as detailed or well-organized
  • Some hooks may be overly specific or less commonly used

Code Comparison

beautiful-react-hooks:

import { useWindowResize } from 'beautiful-react-hooks';

const MyComponent = () => {
  const { width, height } = useWindowResize();
  return <div>Window size: {width}x{height}</div>;
};

usehooks:

import { useWindowSize } from 'usehooks';

const MyComponent = () => {
  const { width, height } = useWindowSize();
  return <div>Window size: {width}x{height}</div>;
};

Both libraries offer similar functionality for common hooks, but beautiful-react-hooks tends to provide more specialized hooks for specific use cases. usehooks focuses on a smaller set of widely-used hooks with clear, concise implementations. The choice between the two may depend on the specific needs of your project and your preference for a more comprehensive library versus a more focused one.

A collection of useful React hooks

Pros of react-hanger

  • More comprehensive set of hooks, including array and object manipulation
  • Actively maintained with recent updates
  • Includes TypeScript support out of the box

Cons of react-hanger

  • Larger bundle size due to more hooks and features
  • Steeper learning curve for developers new to custom hooks
  • Less focus on simplicity and ease of use

Code Comparison

react-hanger:

import { useInput, useBoolean, useNumber } from 'react-hanger';

const MyComponent = () => {
  const name = useInput('');
  const isVisible = useBoolean(false);
  const count = useNumber(0);
};

usehooks:

import { useInput, useToggle, useCounter } from 'usehooks';

const MyComponent = () => {
  const [name, setName] = useInput('');
  const [isVisible, toggleVisible] = useToggle(false);
  const [count, { increment, decrement }] = useCounter(0);
};

Both libraries provide similar functionality, but react-hanger offers a more object-oriented approach with methods attached to the returned values. usehooks, on the other hand, follows a more traditional React hooks pattern with separate state and updater functions.

react-hanger is better suited for complex applications requiring a wide range of custom hooks, while usehooks is ideal for simpler projects prioritizing ease of use and a smaller bundle size.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

NPM DownloadsLast 30 Days