Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-async-hook

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-async-hook

Async hook

  • 2.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
98K
decreased by-12.51%
Maintainers
1
Weekly downloads
 
Created

What is react-async-hook?

The react-async-hook package provides a set of hooks to manage asynchronous operations in React components. It simplifies handling async functions, loading states, and errors, making it easier to work with data fetching and other async tasks in a React application.

What are react-async-hook's main functionalities?

useAsync

The `useAsync` hook is used to handle asynchronous operations. It manages the loading state, error state, and the result of the async function. In this example, `useAsync` is used to fetch data from an API and display it.

```javascript
import React from 'react';
import { useAsync } from 'react-async-hook';

const fetchData = async () => {
  const response = await fetch('https://api.example.com/data');
  if (!response.ok) throw new Error('Network response was not ok');
  return response.json();
};

const MyComponent = () => {
  const { loading, error, result } = useAsync(fetchData, []);

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error.message}</div>;
  return <div>Data: {JSON.stringify(result)}</div>;
};

export default MyComponent;
```

useAsyncCallback

The `useAsyncCallback` hook is used to handle asynchronous operations that are triggered by user actions, such as button clicks. It provides an `execute` function to trigger the async operation and manages the loading and error states.

```javascript
import React from 'react';
import { useAsyncCallback } from 'react-async-hook';

const saveData = async (data) => {
  const response = await fetch('https://api.example.com/save', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(data),
  });
  if (!response.ok) throw new Error('Network response was not ok');
  return response.json();
};

const MyComponent = () => {
  const { execute, loading, error } = useAsyncCallback(saveData);

  const handleSave = () => {
    execute({ key: 'value' });
  };

  return (
    <div>
      <button onClick={handleSave} disabled={loading}>Save</button>
      {loading && <div>Saving...</div>}
      {error && <div>Error: {error.message}</div>}
    </div>
  );
};

export default MyComponent;
```

Other packages similar to react-async-hook

Keywords

FAQs

Package last updated on 12 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc