Skip to content

Comments

Fixes #62- Add useFetch Custom hook#68

Open
rray524 wants to merge 1 commit intomainfrom
rray524/create-usefetch-hook
Open

Fixes #62- Add useFetch Custom hook#68
rray524 wants to merge 1 commit intomainfrom
rray524/create-usefetch-hook

Conversation

@rray524
Copy link
Collaborator

@rray524 rray524 commented Dec 24, 2024

Checklist:

  • Added useFetch custom hook

Resolves #62

How to use this useFetch hook?

Usage Example of Theme Toggling:

import useFetch from './useFetch';

const MyComponent = () => {
  const { data, error, loading, refetch } = useFetch('https://api.example.com/data', { method: 'GET' });

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

  return (
    <div>
      <h1>Data</h1>
      <pre>{JSON.stringify(data, null, 2)}</pre>
      <button onClick={refetch}>Refetch</button>
    </div>
  );
};

export default MyComponent;

Description

The useFetch hook is a reusable custom React hook for making API requests. It supports various HTTP methods like GET, POST, PUT, DELETE, and PATCH, while managing loading, error, and data states. With built-in flexibility for auto-fetching and a refetch method, it simplifies API interactions for components. Developers can use it to streamline data fetching and improve code readability.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Create custom useFetch hook

2 participants