Convert Figma logo to code with AI

antonioru logobeautiful-react-hooks

🔥 A collection of beautiful and (hopefully) useful React hooks to speed-up your components and hooks development 🔥

8,182
579
8,182
10

Top Related Projects

41,752

React Hooks — 👍

Awesome React Hooks

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

A collection of useful React hooks

1,906

React hooks done right, for browser and SSR.

13,985

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

Quick Overview

Beautiful React Hooks is a collection of lightweight and reusable React hooks designed to speed up component development and enhance code readability. It provides a set of utility hooks that handle common tasks such as state management, DOM manipulation, and performance optimization, allowing developers to write cleaner and more efficient React code.

Pros

  • Simplifies common React patterns and reduces boilerplate code
  • Improves code readability and maintainability
  • Lightweight and modular, allowing for selective imports
  • Well-documented with clear examples and TypeScript support

Cons

  • Some hooks may have limited use cases or overlap with existing React features
  • Potential performance overhead for very simple components
  • Learning curve for developers unfamiliar with custom hooks
  • Dependency on external library may increase project complexity

Code Examples

  1. Using the useWindowResize hook to track window dimensions:
import { useWindowResize } from 'beautiful-react-hooks';

const MyComponent = () => {
  const { width, height } = useWindowResize();
  
  return (
    <div>
      Window size: {width} x {height}
    </div>
  );
};
  1. Implementing a debounced input with useDebounce:
import { useDebounce } from 'beautiful-react-hooks';

const SearchComponent = () => {
  const [searchTerm, setSearchTerm] = useState('');
  const debouncedSearch = useDebounce(searchTerm, 300);

  useEffect(() => {
    // Perform search with debouncedSearch
  }, [debouncedSearch]);

  return (
    <input
      type="text"
      value={searchTerm}
      onChange={(e) => setSearchTerm(e.target.value)}
    />
  );
};
  1. Using useGeolocation to get user's location:
import { useGeolocation } from 'beautiful-react-hooks';

const LocationComponent = () => {
  const { isSupported, position, error } = useGeolocation();

  if (!isSupported) return <div>Geolocation is not supported</div>;
  if (error) return <div>Error: {error.message}</div>;

  return (
    <div>
      Latitude: {position.latitude}, Longitude: {position.longitude}
    </div>
  );
};

Getting Started

To start using Beautiful React Hooks, first install the package:

npm install beautiful-react-hooks

Then, import and use the desired hooks in your React components:

import React from 'react';
import { useWindowResize, useGeolocation } from 'beautiful-react-hooks';

const MyComponent = () => {
  const { width, height } = useWindowResize();
  const { position } = useGeolocation();

  return (
    <div>
      <p>Window size: {width} x {height}</p>
      <p>Location: {position?.latitude}, {position?.longitude}</p>
    </div>
  );
};

export default MyComponent;

Competitor Comparisons

41,752

React Hooks — 👍

Pros of react-use

  • Larger collection of hooks (100+) covering a wide range of use cases
  • More frequent updates and active maintenance
  • Extensive documentation with examples for each hook

Cons of react-use

  • Larger bundle size due to the extensive collection of hooks
  • Some hooks may have a steeper learning curve for beginners
  • Potential performance impact when importing the entire library

Code Comparison

react-use:

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

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

beautiful-react-hooks:

import { useToggle, useArrayState } from 'beautiful-react-hooks';

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

Both libraries offer similar functionality, but react-use provides a more extensive set of hooks with slightly different naming conventions. The code structure and usage are comparable, making it easy for developers to switch between the two libraries if needed.

Awesome React Hooks

Pros of awesome-react-hooks

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

Cons of awesome-react-hooks

  • Not a standalone library, just a curated list of hooks
  • Requires additional effort to integrate hooks from different sources
  • May include outdated or unmaintained hooks

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>;
};

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

import { useWindowSize } from '@react-hook/window-size';

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

Summary

beautiful-react-hooks is a standalone library offering a collection of custom hooks, while awesome-react-hooks is a curated list of hooks from various sources. beautiful-react-hooks provides a consistent API and documentation, whereas awesome-react-hooks offers a wider variety of hooks but requires more effort to integrate. The choice between them depends on whether you prefer a single, cohesive library or a diverse collection of hooks from different sources.

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

Pros of usehooks

  • More extensive collection of hooks (30+ vs. 20+)
  • Includes TypeScript definitions for better type safety
  • Regular updates and active maintenance

Cons of usehooks

  • Less detailed documentation for each hook
  • Fewer examples and use cases provided
  • No internationalization support

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 usehooks provides a broader range of hooks and better TypeScript support. beautiful-react-hooks, on the other hand, offers more comprehensive documentation and examples for each hook. The code comparison shows that both libraries have similar syntax and usage patterns, making it easy for developers to switch between them if needed.

A collection of useful React hooks

Pros of react-hanger

  • More comprehensive set of hooks, including array and object manipulation
  • Better TypeScript support with stricter typing
  • More active development and frequent updates

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 UI-specific hooks compared to beautiful-react-hooks

Code Comparison

react-hanger:

const [value, setValue, resetValue] = useInput('initial value');
const [count, { increment, decrement }] = useCounter(0);
const [array, { push, pop, shift, unshift }] = useArray([1, 2, 3]);

beautiful-react-hooks:

const [value, setValue] = useInputValue('initial value');
const [count, increment, decrement] = useCounter(0);
const isOnline = useOnlineState();

Both libraries provide similar functionality for basic hooks like useInput and useCounter. However, react-hanger offers more advanced hooks for array manipulation, while beautiful-react-hooks focuses on simpler, UI-oriented hooks like useOnlineState.

react-hanger is better suited for complex state management scenarios, while beautiful-react-hooks excels in providing a set of easy-to-use hooks for common UI patterns. Developers should choose based on their project requirements and familiarity with custom hooks.

1,906

React hooks done right, for browser and SSR.

Pros of react-hookz/web

  • More extensive collection of hooks (60+ vs 30+)
  • Better TypeScript support with stricter types
  • More frequent updates and active maintenance

Cons of react-hookz/web

  • Steeper learning curve due to larger API surface
  • Less focus on simplicity and ease of use
  • Potentially higher bundle size due to more hooks

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>;
};

react-hookz/web:

import { useWindowSize } from '@react-hookz/web';

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

Both libraries offer similar functionality for common use cases, but react-hookz/web provides a more comprehensive set of hooks with stronger typing. beautiful-react-hooks focuses on simplicity and ease of use, making it potentially more suitable for smaller projects or developers new to React hooks. react-hookz/web, on the other hand, offers a wider range of hooks and more advanced features, making it a better choice for larger, more complex applications or developers who need more fine-grained control over their hooks.

13,985

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
  • Actively maintained by Alibaba, with frequent updates

Cons of hooks

  • Larger package size due to more hooks
  • Steeper learning curve with more complex hooks
  • Some hooks may be too specific for general use cases

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>;
};

hooks:

import { useSize } from 'ahooks';

const MyComponent = () => {
  const size = useSize(document.body);
  return <div>Window size: {size.width}x{size.height}</div>;
};

Summary

Both beautiful-react-hooks and hooks provide useful React hooks for common tasks. beautiful-react-hooks offers a simpler, more focused set of hooks, while hooks provides a more extensive collection with better documentation and support. The choice between them depends on project requirements and developer preferences. beautiful-react-hooks may be more suitable for smaller projects or those new to custom hooks, while hooks offers more advanced functionality for larger applications.

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

CI/CD Coverage StatusLicense:
MIT npm GitHub stars

Beautiful React Hooks


A collection of tailor-made React hooks to enhance your development process and make it faster.

Usage example

🇬🇧 English | 🇨🇳 简体中文 | 🇮🇹 Italiano | 🇪🇸 Español | 🇺🇦 Ukrainian | 🇧🇷 Brazilian Portuguese | 🇵🇱 Polski | 🇯🇵 日本語 | 🇹🇷 Türkçe

💡 Why?

Custom React hooks allow developers to abstract the business logic of components into single, reusable functions.
I have noticed that many of the hooks I have created and shared across projects involve callbacks, references, events, and dealing with the component lifecycle.
Therefore, I have created beautiful-react-hooks, a collection of useful React hooks that may help other developers speed up their development process.
Moreover, I have strived to create a concise and practical API that emphasizes code readability, while keeping the learning curve as low as possible, making it suitable for larger teams to use and share t -- Please before using any hook, read its documentation! --

☕️ Features

  • Concise API
  • Small and lightweight
  • Easy to learn

🕺 Install

by using npm:

$ npm install beautiful-react-hooks

by using yarn:

$ yarn add beautiful-react-hooks

Basic usage

importing a hooks is as easy as the following straightforward line:

import useSomeHook from 'beautiful-react-hooks/useSomeHook'

🎨 Hooks

Peer dependencies

Some hooks are built using third-party libraries (such as rxjs, react-router-dom, redux). As a result, you will see these libraries listed as peer dependencies.
Unless you are using these hooks directly, you need not install these dependencies.

Contributing

Contributions are very welcome and wanted.

To submit your custom hook, make sure you have thoroughly read and understood the CONTRIBUTING guidelines.

Prior to submitting your pull request: please take note of the following

  1. make sure to write tests for your code, run npm test and npm build before submitting your merge request.
  2. in case you're creating a custom hook, make sure you've added the documentation (you may use the HOOK_DOCUMENTATION_TEMPLATE to document your custom hook).

Credits

Icon made by Freepik from www.flaticon.com

NPM DownloadsLast 30 Days