Managing asynchronous data in React components often requires repetitive state management for loading, errors, and resolved data.

I built xansync to make this process simpler.

xansync is a lightweight React utility library that provides a simple hook and a higher-order component for handling asynchronous operations.

Why xansync?

Instead of repeatedly writing useState, useEffect, and async request logic, you can use a single hook to manage everything.

Installation

npm install xansync 

Using useSync

import { useSync } from "xansync"; function Posts() { const { data, isLoading, error, reload } = useSync( async () => { const response = await fetch( "https://jsonplaceholder.typicode.com/posts" ); if (!response.ok) { throw new Error("Failed to fetch posts"); } return response.json(); }, [] ); if (error) return <p>{error.message}</p>; return ( <div> {isLoading && <p>Loading...</p>} {data?.map((post) => ( <article key={post.id}> <h2>{post.title}</h2> </article> ))} <button onClick={reload}>Reload</button> </div> ); } 

Features

  • Simple asynchronous data management.
  • Built-in loading and error states.
  • Reload functionality.
  • Direct data updates with setData.
  • Dependency-based execution.
  • Prevents outdated async results from updating state.
  • Higher-order component with withAsync.

Higher-order component

If you prefer a component-based approach, xansync also provides withAsync for loading data and rendering the resolved value.

Explore xansync

Website: https://xansync.devnax.com

GitHub: https://github.com/devnax/xansync

npm: https://www.npmjs.com/package/xansync

I'd appreciate feedback from the React community. What do you think about this approach to asynchronous data management?