npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

use-axios-with-redux

v1.0.11

Published

A react hook to aggregate axios network request an react redux

Readme

use-axios-with-redux custom hook

A react hook to aggregate axios network requests and functionalities with react redux

The purpose of this hook is to create a reusable flexible hook that can handle network requests and cancelation (using Axios) and storing the response (using react redux).

Installation

    npm install @types/axios use-axios-with-redux

Usage

Customizing the hook

The first step is to customize the hook to work with your project redux setup.

After setting up redux store by following the guide on react-redux documentation, create a new hook useNetworkRequest in your project.

Then import the use-axios-with-redux hook in the custom hook:

import useAxiosWithRedux from "use-axios-with-redux";

(Typescript only)

Import type for typescript:

import { NetworkRequestType } from "use-axios-with-redux";

Add the following lines of code to your file:

Typescript:

export const useNetworkRequest = <RequestDataType, ResponseDataType>(
  url: string,
  config?: AxiosRequestConfig,
  reduxConfig?: ReduxConfigType
): NetworkRequestType<RequestDataType, ResponseDataType> => {
  return useAxiosWithRedux<RequestDataType, ResponseDataType>(
    url,
    {
      ...(reduxConfig || {}),
      useSelector: useSelector, // Add a react redux selector hook
      appDispatch: useDispatch(), // Add a react redux dispatch object
    },
    config
  );
};

Javascript

export const useNetworkRequest = (url, config, reduxConfig) => {
  return useAxiosWithRedux(
    url,
    {
      ...(reduxConfig || {}),
      useSelector: useSelector, // Add a react redux selector hook
      appDispatch: useDispatch(), // Add a react redux dispatch object
    },
    config
  );
};

You are all set.

To use this custom hook, call the function with the following:

url // Url endpoint (equivalent to url parameter in axios)
config // equivalent to axios config object
reduxConfig = {
    action //redux action
    callback //callback that you would normally supply to useSelector hook eg state=>state.auth
}

The returns an array with the following stucture:

[
  {
    data, // Network response or current redux state
    error, // Axios response
    isLoading, // Request loading state (true/false)
  },
  dispatch, // A function to perform the network request
  reset, // Set data = null, error=null, isLoading: false
  cancel, // Cancel a request
  dataPromise, // Axios promise for the request
];

Example


...

const [{ data: response, isLoading, error }, sendRequest, cancel, promise] =
  useNetworkRequest<RequestType, ResponseType>(
    "https://www.google.com",
    { method: "POST" },
    { action: REDUX_ACTION_TYPE, callback: (state) => state.google }
  );

  ...
return (
    <>
        <p>{response}</p>
        <button onClick={sendRequest}>Click me</button>
    </>
...
)