4.0.2 • Published 2 months ago

use-fetch-query v4.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

use-fetch-query

use-fetch-query is a lightweight custom hook for making HTTP requests using the fetch API in React applications.

Installation

You can install the package via npm:

npm i use-fetch-query

Usage

import { useQuery } from "use-fetch-query";

function MyComponent() {
  const { data, error, isLoading } = useQuery({
    method: "GET",
    url: "https://api.example.com/data",
  });

  // Handle loading state
  if (isLoading) {
    return <div>Loading...</div>;
  }

  // Handle error
  if (error) {
    return <div>Error: {error}</div>;
  }

  // Render data
  return (
    <div>
      <h1>Data:</h1>
      <pre>{JSON.stringify(data, null, 2)}</pre>
    </div>
  );
}

export default MyComponent;

POST request

function App() {
  const { data, postData } = useQuery();

  useEffect(() => {
    console.log(data, "data");
  }, [data]);

  return (
    <div>
      <button onClick={() => postData({ hi: "send me" })}>Make a post</button>
    </div>
  );
}

QueryProvider

The QueryProvider component is a part of the use-fetch-query package, providing context for making HTTP requests using the fetch API in React applications.

Usage

import App from "./App";
import React from "react";
import ReactDOM from "react-dom/client";
import { QueryProvider, ClientProviderConfig } from "use-fetch-query";

const root = ReactDOM.createRoot(
  document.getElementById("root") as HTMLElement
);

const clientConfig = new ClientProviderConfig({
  url: "http://localhost",
  defaultHeaders: {
    name: "Content-Type",
    value: "application/json",
  },
});

root.render(
  <React.StrictMode>
    <QueryProvider client={clientConfig}>
      <App />
    </QueryProvider>
  </React.StrictMode>
);

Parameters

  • method (string): The HTTP method for the request (default: 'GET').
  • headers (object): Additional headers to include in the request.
  • body (object): The request payload.
  • queryParams (object): Query parameters to append to the URL.
  • url (string): The URL to which the request will be sent.

Contributing

If you encounter any issues or have suggestions for improvements, feel free to contribute by opening an issue or submitting a pull request on GitHub.

4.0.1

2 months ago

4.0.2

2 months ago

3.0.9

2 months ago

3.0.8

2 months ago

3.0.7

2 months ago

3.0.6

2 months ago

3.0.5

2 months ago

4.0.0

2 months ago

2.0.3

2 months ago

2.0.2

2 months ago

2.0.5

2 months ago

2.0.4

2 months ago

2.0.7

2 months ago

2.0.6

2 months ago

2.0.9

2 months ago

2.0.8

2 months ago

3.0.4

2 months ago

3.0.3

2 months ago

3.0.2

2 months ago

3.0.1

2 months ago

3.0.0

2 months ago

2.0.1

2 months ago

1.1.9

2 months ago

1.1.8

2 months ago

1.1.7

2 months ago

1.1.6

2 months ago

2.0.0

2 months ago

1.1.5

2 months ago

1.1.4

2 months ago

1.1.3

2 months ago

1.1.2

2 months ago

1.1.1

2 months ago

1.1.0

2 months ago

1.0.9

2 months ago

1.0.8

2 months ago

1.0.7

2 months ago

1.0.6

2 months ago

1.0.5

2 months ago

1.0.4

2 months ago

1.0.3

2 months ago

1.0.2

2 months ago

1.0.1

2 months ago

1.0.0

2 months ago