1.1.0 โ€ข Published 5 months ago

react-axios-provider v1.1.0

Weekly downloads
105
License
MIT
Repository
-
Last release
5 months ago

react-axios-provider ยท๐Ÿš€

npm version ยท MIT license

axios-provider.jpg

A flexible React Context provider for managing Axios instances throughout your application. This provider allows you to easily share and update an Axios instance across your component tree.

โœจ Features

  • ๐ŸŒ€ Provides a shared Axios instance via React Context
  • ๐Ÿ”„ Allows dynamic updates to the Axios instance at runtime
  • ๐Ÿ“ Includes TypeScript support
  • ๐Ÿ”Œ Simple integration

Installation ๐Ÿ“ฆ

npm install react-axios-provider

๐Ÿ“– Usage

๐ŸŽฏ Basic Setup

First, wrap your application (or a part of it) with the AxiosProvider:

import axios from 'axios';
import { AxiosProvider } from './AxiosProvider';

// Create your Axios instance
const axiosInstance = axios.create({
  baseURL: 'https://api.example.com',
  timeout: 5000,
});

function App() {
  return (
    <AxiosProvider instance={axiosInstance}>
      <YourComponents />
    </AxiosProvider>
  );
}

๐Ÿ”จ Using the Axios Instance

Access the Axios instance in your components using the useAxiosContext hook:

import { useAxiosContext } from './AxiosProvider';

function UserProfile() {
  const { axios } = useAxiosContext();
  
  const fetchUser = async () => {
    try {
      const response = await axios.get('/user/profile');
      // Handle response
    } catch (error) {
      // Handle error
    }
  };
  
  return (
    // Your component JSX
  );
}

๐Ÿ”„ Updating the Axios Instance

You can update the Axios instance at any time (e.g., to update authentication headers):

function AuthManager() {
  const { axios, updateAxios } = useAxiosContext();
  
  const updateToken = (newToken: string) => {
    const newInstance = axios.create({
      ...axios.defaults,
      headers: {
        ...axios.defaults.headers,
        Authorization: `Bearer ${newToken}`,
      },
    });
    
    updateAxios(newInstance);
  };
  
  return (
    // Your component JSX
  );
}

๐Ÿ“š API Reference

๐Ÿ”ง AxiosProvider

Main component that provides the Axios context.

Props

NameTypeDescription
instanceAxiosInstanceThe initial Axios instance to be provided
childrenReactNodeChild components that will have access to the context

๐ŸŽฃ useAxiosContext

A custom hook that provides access to the Axios context.

Returns

NameTypeDescription
axiosAxiosInstanceThe current Axios instance
updateAxios(instance: AxiosInstance) => voidFunction to update the current Axios instance

โš ๏ธ Error Handling

The useAxiosContext hook will throw an error if used outside of an AxiosProvider.

๐Ÿ’ก Best Practices

  1. Single Provider: Place the AxiosProvider as high in your component tree as needed, typically at the root of your application.

  2. Instance Updates: When updating the Axios instance, make sure to preserve any necessary default settings from the previous instance.

  3. Error Boundaries: Consider wrapping your components in an error boundary to catch any errors thrown by useAxiosContext.


Contributing

We love contributions! Please follow these steps:

  1. Fork this repository
  2. Create a new feature branch
  3. Commit your changes
  4. Open a Pull Request

We'll review and merge if it fits the project's scope. ๐Ÿ™Œ


License

This project is licensed under the MIT License.

Happy coding with react-axios-provider! Feel free to share and contribute.

0.0.1

5 months ago

1.1.0

5 months ago

0.0.2

5 months ago

1.0.3

5 months ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago