Convert Figma logo to code with AI

streamich logoreact-use

React Hooks — 👍

41,502
3,130
41,502
590

Top Related Projects

41,619

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.

46,119

🐻 Bear necessities for state management in React

41,619

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.

30,209

React Hooks for Data Fetching

5,493

RFCs for changes to React

Quick Overview

react-use is a comprehensive collection of essential React hooks that aims to enhance the development experience and reduce boilerplate code. It provides a wide range of hooks for various purposes, including state management, side effects, animations, and more, allowing developers to build robust React applications with ease.

Pros

  • Extensive collection of hooks covering many common use cases
  • Well-documented with examples and TypeScript support
  • Actively maintained with frequent updates and contributions
  • Modular design allows for easy cherry-picking of specific hooks

Cons

  • Large number of hooks may be overwhelming for beginners
  • Some hooks might have performance implications if not used carefully
  • Potential dependency on external libraries for certain hooks
  • May increase bundle size if not properly tree-shaken

Code Examples

  1. Using the useToggle hook for a simple toggle state:
import { useToggle } from 'react-use';

const ToggleComponent = () => {
  const [isOn, toggle] = useToggle(false);
  return (
    <button onClick={toggle}>
      {isOn ? 'ON' : 'OFF'}
    </button>
  );
};
  1. Implementing a debounced input with useDebounce:
import { useDebounce } from 'react-use';

const DebouncedInput = () => {
  const [value, setValue] = useState('');
  const debouncedValue = useDebounce(value, 500);

  return (
    <input
      value={value}
      onChange={(e) => setValue(e.target.value)}
      placeholder="Type to search..."
    />
  );
};
  1. Using useLocalStorage for persistent state:
import { useLocalStorage } from 'react-use';

const PersistentCounter = () => {
  const [count, setCount] = useLocalStorage('counter', 0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
};

Getting Started

To start using react-use in your project:

  1. Install the package:

    npm install react-use
    
  2. Import and use hooks in your components:

    import { useToggle, useDebounce } from 'react-use';
    
    const MyComponent = () => {
      const [isVisible, toggle] = useToggle(false);
      const debouncedValue = useDebounce(someValue, 300);
    
      // Use the hooks in your component logic
    };
    
  3. Refer to the official documentation for detailed usage instructions and available hooks.

Competitor Comparisons

41,619

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.

Pros of TanStack Query

  • Specialized for data fetching and state management
  • Built-in caching and automatic background updates
  • Extensive documentation and strong community support

Cons of TanStack Query

  • Steeper learning curve for beginners
  • More opinionated approach to data management
  • Limited to data fetching and caching use cases

Code Comparison

react-use:

import { useAsync } from 'react-use';

const MyComponent = () => {
  const state = useAsync(async () => {
    const response = await fetch('https://api.example.com/data');
    return response.json();
  }, []);

  if (state.loading) return 'Loading...';
  if (state.error) return 'Error!';
  return <div>{JSON.stringify(state.value)}</div>;
};

TanStack Query:

import { useQuery } from '@tanstack/react-query';

const MyComponent = () => {
  const { isLoading, error, data } = useQuery('myData', async () => {
    const response = await fetch('https://api.example.com/data');
    return response.json();
  });

  if (isLoading) return 'Loading...';
  if (error) return 'Error!';
  return <div>{JSON.stringify(data)}</div>;
};

Summary

TanStack Query excels in data fetching and state management with built-in caching, while react-use offers a broader set of utility hooks for various React use cases. TanStack Query provides a more specialized solution for data-heavy applications, whereas react-use is more versatile for general React development needs.

46,119

🐻 Bear necessities for state management in React

Pros of zustand

  • Simpler API with less boilerplate compared to react-use
  • Built-in support for middleware and devtools
  • Better performance for large state updates

Cons of zustand

  • Less comprehensive collection of hooks and utilities
  • Steeper learning curve for developers new to state management
  • Limited built-in TypeScript support compared to react-use

Code Comparison

zustand:

import create from 'zustand'

const useStore = create(set => ({
  count: 0,
  increment: () => set(state => ({ count: state.count + 1 })),
}))

react-use:

import { useCounter } from 'react-use'

const [count, { inc }] = useCounter(0)

Summary

zustand offers a more streamlined approach to state management with better performance for complex state updates. It provides built-in middleware support and devtools integration. However, it has a steeper learning curve and fewer utility hooks compared to react-use.

react-use, on the other hand, provides a comprehensive collection of hooks and utilities, making it easier for developers to adopt and use in various scenarios. It has better TypeScript support out of the box but may not perform as well for large state updates.

The choice between the two depends on the specific needs of your project, such as the complexity of state management required and the preference for a more opinionated (zustand) or flexible (react-use) approach.

41,619

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.

Pros of TanStack Query

  • Specialized for data fetching and state management
  • Built-in caching and automatic background updates
  • Extensive documentation and strong community support

Cons of TanStack Query

  • Steeper learning curve for beginners
  • More opinionated approach to data management
  • Limited to data fetching and caching use cases

Code Comparison

react-use:

import { useAsync } from 'react-use';

const MyComponent = () => {
  const state = useAsync(async () => {
    const response = await fetch('https://api.example.com/data');
    return response.json();
  }, []);

  if (state.loading) return 'Loading...';
  if (state.error) return 'Error!';
  return <div>{JSON.stringify(state.value)}</div>;
};

TanStack Query:

import { useQuery } from '@tanstack/react-query';

const MyComponent = () => {
  const { isLoading, error, data } = useQuery('myData', async () => {
    const response = await fetch('https://api.example.com/data');
    return response.json();
  });

  if (isLoading) return 'Loading...';
  if (error) return 'Error!';
  return <div>{JSON.stringify(data)}</div>;
};

Summary

TanStack Query excels in data fetching and state management with built-in caching, while react-use offers a broader set of utility hooks for various React use cases. TanStack Query provides a more specialized solution for data-heavy applications, whereas react-use is more versatile for general React development needs.

30,209

React Hooks for Data Fetching

Pros of SWR

  • Focused specifically on data fetching and caching
  • Built-in support for real-time and optimistic updates
  • Lightweight and easy to integrate with existing projects

Cons of SWR

  • Limited to data fetching use cases
  • Requires more setup for complex scenarios
  • Less flexibility compared to react-use's diverse hook collection

Code Comparison

SWR:

import useSWR from 'swr'

function Profile() {
  const { data, error } = useSWR('/api/user', fetcher)
  if (error) return <div>Failed to load</div>
  if (!data) return <div>Loading...</div>
  return <div>Hello {data.name}!</div>
}

react-use:

import { useAsync } from 'react-use'

function Profile() {
  const state = useAsync(async () => {
    const response = await fetch('/api/user')
    return response.json()
  })
  if (state.loading) return <div>Loading...</div>
  if (state.error) return <div>Failed to load</div>
  return <div>Hello {state.value.name}!</div>
}

SWR is more specialized for data fetching, offering built-in caching and real-time updates. react-use provides a wider range of hooks for various use cases, including data fetching. SWR's API is simpler for basic data fetching scenarios, while react-use offers more flexibility and control over the fetching process.

5,493

RFCs for changes to React

Pros of rfcs

  • Official React repository for discussing and proposing changes
  • Provides a structured process for community input on React's future
  • Offers in-depth technical discussions and rationales for proposed features

Cons of rfcs

  • Focused on proposals rather than ready-to-use code
  • May not provide immediate solutions for developers' current needs
  • Can be more theoretical and less practical for day-to-day development

Code comparison

rfcs typically doesn't contain implementation code, but rather proposal descriptions. react-use, on the other hand, provides ready-to-use custom hooks:

react-use:

import { useToggle } from 'react-use';

const [isOn, toggleIsOn] = useToggle(false);

rfcs:

# RFC: New Hook Proposal

## Summary

This RFC proposes a new React hook called `useToggle`...

Summary

rfcs is an official React repository for discussing and proposing changes to the React library. It offers a structured process for community input and in-depth technical discussions. However, it focuses on proposals rather than ready-to-use code and may not provide immediate solutions for developers.

react-use, in contrast, is a collection of ready-to-use custom React hooks that developers can immediately implement in their projects. It offers practical solutions for common use cases but may not provide the same level of in-depth discussion or official backing as rfcs.

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



👍
react-use





npm package CircleCI master npm downloads demos
Collection of essential React Hooks. Port of libreact.
Translations: 🇨🇳 汉语




npm i react-use












Usage — how to import.
Unlicense — public domain.
Support — add yourself to backer list below.






Contributors








NPM DownloadsLast 30 Days