axios-react-hook 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MUHAMMED RASHID
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # axios-react-hook
2
+
3
+ A simple and customizable React hook for making HTTP requests using Axios.
4
+
5
+ ## Features
6
+
7
+ - ✅ Supports GET, POST, PUT, DELETE, PATCH, etc.
8
+ - ✅ Auto and manual fetching
9
+ - ✅ Refetch support
10
+ - ✅ Pass your own Axios instance
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install axios-react-hook
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ```js
21
+ import useAxios from 'axios-react-hook';
22
+ import axiosInstance from './axiosInstance'; // Optional custom instance
23
+
24
+ const { data, loading, error, refetch } = useAxios(
25
+ 'https://jsonplaceholder.typicode.com/posts',
26
+ 'get',
27
+ null,
28
+ true,
29
+ axiosInstance
30
+ );
31
+ ```
32
+
33
+ ## API
34
+
35
+ ### Parameters
36
+
37
+ | Param | Type | Default | Description |
38
+ |---------------|----------|---------|----------------------------------------|
39
+ | `url` | `string` | — | Request URL |
40
+ | `method` | `string` | `'get'` | HTTP method |
41
+ | `data` | `object` | `null` | Request payload or query params |
42
+ | `auto` | `boolean`| `true` | Auto execute request on mount |
43
+ | `instance` | `AxiosInstance` | `axios` | Custom Axios instance (optional) |
44
+
45
+ ## License
46
+
47
+ MIT
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "axios-react-hook",
3
+ "version": "1.0.0",
4
+ "description": "A lightweight React hook for Axios-based API calls with support for all HTTP methods.",
5
+ "main": "src/useAxios.js",
6
+ "keywords": ["axios", "react", "hook", "api", "http"],
7
+ "author": "Muhammed Rashid",
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/devmdrd/axios-react-hook"
12
+ },
13
+ "dependencies": {
14
+ "axios": "^1.6.0"
15
+ }
16
+ }
@@ -0,0 +1,33 @@
1
+ import { useState, useEffect } from 'react';
2
+ import axios from 'axios';
3
+
4
+ export default function useAxios(
5
+ url,
6
+ method = 'get',
7
+ body = null,
8
+ auto = true,
9
+ instance = axios
10
+ ) {
11
+ const [data, setData] = useState(null);
12
+ const [loading, setLoading] = useState(auto);
13
+ const [error, setError] = useState(null);
14
+
15
+ const fetch = async () => {
16
+ setLoading(true);
17
+ setError(null);
18
+ try {
19
+ const res = await instance[method](url, body);
20
+ setData(res.data);
21
+ } catch (err) {
22
+ setError(err);
23
+ } finally {
24
+ setLoading(false);
25
+ }
26
+ };
27
+
28
+ useEffect(() => {
29
+ if (auto) fetch();
30
+ }, [url]);
31
+
32
+ return { data, loading, error, refetch: fetch };
33
+ }