DEV Community

Zeeshan Haider Shaheen
Zeeshan Haider Shaheen

Posted on • Updated on

Introducing react-use-apifetcher: Simplify Your API Calls in React

In the ever-evolving world of web development, React remains a cornerstone for building dynamic user interfaces. However, as projects grow in complexity, managing API calls can become cumbersome, leading to repetitive code and challenging state management scenarios. To address this, we're excited to introduce react-use-apifetcher, a custom React hook designed to simplify fetching data from APIs while providing flexible data processing capabilities.

Why react-use-apifetcher?

While React offers the powerful useEffect hook for side effects, such as data fetching, developers often find themselves writing boilerplate code to handle loading states, errors, and data transformation. react-use-apifetcher abstracts these concerns, offering a concise and reusable solution for making API requests within your React components.

Features

  • Simplified State Management: Automatically handles loading and error states, allowing you to focus on what matters: your application's logic and UI.
  • Data Processing Callback: Optionally transform or filter your data using a callback function before setting the component's state, all within the same hook call.
  • TypeScript Support: Fully typed for a better development experience, ensuring type safety for your API responses and processed data.

Getting Started

Installation

Begin by installing react-use-apifetcher in your React project:

npm install react-use-apifetcher
Enter fullscreen mode Exit fullscreen mode

Usage

Without Callback Function

Fetch data effortlessly without additional processing:

import React from 'react';
import { useFetcher } from 'react-use-apifetcher';

const PostsComponent: React.FC = () => {
  const { data: posts, loading, error } = useFetcher('https://jsonplaceholder.typicode.com/posts');

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

  return (
    <ul>
      {posts?.map(post => <li key={post.id}>{post.title}</li>)}
    </ul>
  );
};

Enter fullscreen mode Exit fullscreen mode

With Callback Function
Customize your data with a processing callback:

import React from 'react';
import { useFetcher } from 'react-use-apifetcher';

const FilteredPostsComponent: React.FC = () => {
  const processData = (posts) => posts.filter(post => post.userId < 5);

  const { data: filteredPosts, loading, error } = useFetcher('https://jsonplaceholder.typicode.com/posts', {}, processData);

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

  return (
    <ul>
      {filteredPosts?.map(post => <li key={post.id}>{post.title}</li>)}
    </ul>
  );
};

Enter fullscreen mode Exit fullscreen mode

Under the Hood

react-use-apifetcher leverages the Fetch API under the hood, wrapping it in a React hook that manages the request's lifecycle. By providing a callback function, you can easily integrate data processing steps within the fetch operation, streamlining your component's logic.

Conclusion

react-use-apifetcher is more than just a utility; it's a pattern for simplifying data fetching in React applications. By abstracting the repetitive aspects of API calls, it allows developers to write cleaner, more readable code. We encourage you to integrate react-use-apifetcher into your next project and contribute to its growth.

Happy coding!

📚 Check it out and let's make react-use-apifetcher even better together:
npm
github

Top comments (0)