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
Enter fullscreen mode
Exit fullscreen mode
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 ? .…