Convert Figma logo to code with AI

jaredpalmer logoreact-fns

Browser API's turned into declarative React components and HoC's

3,729
100
3,729
3

Top Related Projects

41,752

React Hooks — 👍

Awesome React Hooks

42,148

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

📋 React Hooks for form state management and validation (Web + React Native)

48,653

🐻 Bear necessities for state management in React

30,395

React Hooks for Data Fetching

Quick Overview

React-fns is a collection of React hooks and components that provide easy access to browser APIs and common web functionality. It aims to simplify working with browser events, device information, and other web-related features in React applications.

Pros

  • Simplifies access to browser APIs and common web functionality
  • Provides a set of reusable hooks and components for common tasks
  • Reduces boilerplate code in React applications
  • Improves code readability and maintainability

Cons

  • Some hooks may have performance implications if not used carefully
  • Limited documentation and examples for some features
  • May introduce unnecessary dependencies for simple projects
  • Some functionality might be achievable with native browser APIs or other lightweight libraries

Code Examples

  1. Using the useWindowSize hook to get window dimensions:
import { useWindowSize } from 'react-fns';

function MyComponent() {
  const { width, height } = useWindowSize();
  return (
    <div>
      Window size: {width} x {height}
    </div>
  );
}
  1. Using the useNetworkStatus hook to check network connectivity:
import { useNetworkStatus } from 'react-fns';

function NetworkIndicator() {
  const { online } = useNetworkStatus();
  return (
    <div>
      Network status: {online ? 'Online' : 'Offline'}
    </div>
  );
}
  1. Using the useGeolocation hook to get the user's location:
import { useGeolocation } from 'react-fns';

function LocationDisplay() {
  const { latitude, longitude } = useGeolocation();
  return (
    <div>
      Current location: {latitude}, {longitude}
    </div>
  );
}

Getting Started

To use react-fns in your project, follow these steps:

  1. Install the package:

    npm install react-fns
    
  2. Import and use the desired hooks or components in your React components:

    import { useWindowSize, useNetworkStatus } from 'react-fns';
    
    function MyComponent() {
      const { width, height } = useWindowSize();
      const { online } = useNetworkStatus();
    
      return (
        <div>
          Window size: {width} x {height}
          Network status: {online ? 'Online' : 'Offline'}
        </div>
      );
    }
    
  3. Make sure to wrap your app or component tree with the necessary providers if required by specific hooks or components.

Competitor Comparisons

41,752

React Hooks — 👍

Pros of react-use

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

Cons of react-use

  • Potentially larger bundle size due to the extensive collection of hooks
  • May require more time to learn and navigate the extensive API

Code Comparison

react-fns:

import { WindowSize } from 'react-fns';

<WindowSize>
  {({ width, height }) => (
    <div>Window size: {width} x {height}</div>
  )}
</WindowSize>

react-use:

import { useWindowSize } from 'react-use';

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

Both libraries provide similar functionality, but react-use offers a more modern hooks-based approach, while react-fns uses render props. react-use's implementation is more concise and aligns better with current React best practices.

Awesome React Hooks

Pros of awesome-react-hooks

  • Comprehensive collection of React hooks from various sources
  • Regularly updated with new hooks and resources
  • Community-driven project with contributions from multiple developers

Cons of awesome-react-hooks

  • Not a standalone library, requires additional setup to use individual hooks
  • May include hooks of varying quality or maintenance levels
  • Lacks standardized documentation across all listed hooks

Code Comparison

react-fns:

import { WindowSize } from 'react-fns';

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

awesome-react-hooks (using a hook from the list):

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

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

Summary

awesome-react-hooks serves as a curated list of React hooks, offering a wide variety of options for developers. It's continuously updated but requires more effort to implement specific hooks. react-fns, on the other hand, provides a self-contained set of utilities with consistent documentation, though it may have a more limited scope compared to the extensive collection in awesome-react-hooks.

42,148

🤖 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 Query

  • More comprehensive data fetching and state management solution
  • Better caching and synchronization capabilities
  • Active development and larger community support

Cons of Query

  • Steeper learning curve due to more complex API
  • Potentially overkill for simple use cases
  • Requires additional setup and configuration

Code Comparison

react-fns:

import { WindowSize } from 'react-fns';

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

Query:

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

const MyComponent = () => {
  const { data, isLoading, error } = useQuery('userData', fetchUserData);
  if (isLoading) return 'Loading...';
  if (error) return 'An error occurred';
  return <div>{data.name}</div>;
};

Summary

Query is a more powerful and feature-rich solution for data fetching and state management, while react-fns focuses on providing utility components for common React patterns. Query offers better caching and synchronization but comes with a steeper learning curve. react-fns is simpler to use but has limited functionality compared to Query. The choice between the two depends on the specific needs of your project and the complexity of your data management requirements.

📋 React Hooks for form state management and validation (Web + React Native)

Pros of react-hook-form

  • More focused on form handling and validation
  • Offers better performance with minimal re-renders
  • Provides a simpler API with less boilerplate code

Cons of react-hook-form

  • Limited to form-related functionality
  • May require additional libraries for complex form scenarios
  • Steeper learning curve for developers new to React Hooks

Code Comparison

react-hook-form:

import { useForm } from "react-hook-form";

const { register, handleSubmit } = useForm();
const onSubmit = data => console.log(data);

<form onSubmit={handleSubmit(onSubmit)}>
  <input {...register("firstName")} />
  <input type="submit" />
</form>

react-fns:

import { WindowSize } from "react-fns";

<WindowSize>
  {({ width, height }) => (
    <div>Window size: {width} x {height}</div>
  )}
</WindowSize>

react-fns is a collection of React components and hooks for common web APIs and browser events, while react-hook-form is specifically designed for form handling in React applications. react-hook-form offers a more streamlined approach to form management with better performance, while react-fns provides a broader set of utilities for various browser-related functionalities. The choice between the two depends on the specific needs of your project and whether you require focused form handling or a more general set of React utilities.

48,653

🐻 Bear necessities for state management in React

Pros of zustand

  • Simpler API with less boilerplate compared to react-fns
  • Better performance due to its minimalistic approach
  • More active development and community support

Cons of zustand

  • Limited built-in functionality for handling side effects
  • Steeper learning curve for developers new to state management

Code Comparison

react-fns:

import { WindowSize } from 'react-fns';

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

zustand:

import create from 'zustand';

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

const Counter = () => {
  const { count, increment } = useStore();
  return <button onClick={increment}>{count}</button>;
};

Summary

zustand offers a more modern and streamlined approach to state management in React applications, with better performance and community support. However, react-fns provides a wider range of built-in utilities for handling common React patterns and side effects. The choice between the two depends on the specific needs of your project and your team's familiarity with state management concepts.

30,395

React Hooks for Data Fetching

Pros of SWR

  • More active development and maintenance
  • Built-in support for real-time data updates and revalidation
  • Extensive documentation and examples

Cons of SWR

  • Steeper learning curve for beginners
  • Potentially more complex setup for simple use cases

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-fns:

import { WindowSize } from 'react-fns'

const App = () => (
  <WindowSize>
    {({ width, height }) => (
      <div>Window size: {width} x {height}</div>
    )}
  </WindowSize>
)

Summary

SWR offers more advanced features for data fetching and state management, while react-fns provides simpler utility components for common React patterns. SWR is better suited for complex applications with real-time data requirements, whereas react-fns may be more appropriate for smaller projects or specific use cases like handling window events or device orientation.

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

repo-banner

react-fns is a collection of imperative Browser API's turned into declarative React components and higher-order components for lots of common situations.

Quick Start

npm i react-fns --save

Resources

Author

Contributors

Thanks goes to these wonderful people (emoji key):


MICHAEL JACKSON

🤔

Pavel Prichodko

💻 📖

Richard Powell

💻

Tim Brown

📖

Jack Moore

💻

Dayle Rees

📖

Thomas Flemming

📖

Sam Kvale

🐛 💻

Rhys Powell

💻

Jeppe Reinhold

📖

Victor Magalhães

🐛 💻

Macklin Underdown

📖

This project follows the all-contributors specification. Contributions of any kind welcome!

NPM DownloadsLast 30 Days