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 🙏

© 2024 – Pkg Stats / Ryan Hefner

reduxios

v2.0.15

Published

Reduces data fetching redux logic by up to 80%

Downloads

5

Readme

reduxios

Utility function for handling reducers and actions related to data fetching

decreases redux data fetching code by about 80%

Example Usage in 4 simple steps

  • Generate the helper with the basename for action types
import reduxios from "reduxios";

export const booksStoreReduxios = reduxios<Book[]>("FETCH_BOOKS");
  • Create the Reducer, which will handle various Fetch states. It takes the initial data as an argument
export const booksReducer = booksStoreReduxios.createReducer([]);
  • Makes the action hook for Fetching your data or calling the API.
import axios from "axios";

export const useResourceBooks = () => {
  return booksStoreReduxios.useResource({
    axiosInstance: axios, // This can also be an axios instance created
    method: "get",
    url: "/books"
  });
};
  • Use the action hook and state in your component. No need to dispatch the action.
const BooksList: FC = () => {
  const getBooks = useResourceBooks();
  const { data, fetchState, axiosErrorResponse } = useSelector(
    (state: RootState) => state.books
  );

  useEffect(() => {
    getBooks();
  }, []);

  return (
    <div>
      <h1>My Book List</h1>
      <ul>
        {data.map(book => (
          <Book key={book.id} book={book} />
        ))}
      </ul>
    </div>
  );
};

That's it! No need to manually write out action creators, type declarations, reducers and data fetching attempt/success/failure/reset handling. You get everything out of the box!

Want More Detailed Explanation?

  • First, you call the function with the action type name,
/*   
Book[] - represents the typeescripts data  Type definition expected from the api 

AxiosErrorResponseData(optional) -  is the Error info sent from the 
 server e.g with res.status(401).send({message:  'unauthorized'}). 
 so, in this case, AxiosErrorResponseData would be: `{message: 
 string}`. 
 This can then be accessed later via axiosError object, that is:
  ```axiosErrorResponse.data``` (which is derived from the 
  catch block error.response.data )

'FETCH_BOOKS' - basename for redux actionTypes. Various types are
 built from this. (i.e FETCH_BOOKS_REQUEST, FETCH_BOOKS_SUCCESS, 
 FETCH_BOOKS_FAILURE, FETCH_BOOKS_RESET_STATE )
 
Returns {object} { useResource, createReducer, useResetState }  
  */
export const booksStoreReduxios = reduxios<Book[], AxiosErrorResponseData>(
  "FETCH_BOOKS"
);

//where Book and AxiosErrorResponseData could be e.g:
interface Book {
  id: string;
  title: string;
  author: string;
}

interface AxiosErrorResponseData {
  message: string;
}
  • This helper can then be used to create the reducer that handles the request, success and failure states of the data.
/*
create reducer handles various Fetch states.
It takes the initial data as an argument
*/
export const booksReducer = booksStoreReduxios.createReducer([]);
  • Makes the api call.
/* useResource takes argument of all axios configurations and also axiosInstance(can also be the defualt axios).
    Returns Custom  react hook which can be used in a component for fetching the data
    */
import axios from "axios";

export const useResourceBooks = () => {
  return booksStoreReduxios.useResource({
    axiosInstance: axios, // This can also be an axios instance created
    method: "get",
    url: "/books"
  });
};
  • This can then be used in a component like this
const BooksList: FC = () => {
  const getBooks = useResourceBooks();
  const { data: markets, fetchState, axiosErrorResponse } = useSelector(
    (state: RootState) => state.books
  );
  /* Where:
   fetchState:  "idle" | "attempt" | "success" | "failure"
   axiosErrorResponse: includes axios error information  and status from the server if 
   the api call fails data from the server
  */

  useEffect(() => {
    getBooks();
  }, []);

  return (
    <div>
      <h1>My Book List</h1>
      <ul>
        {markets.map(book => (
          <Book key={book.id} book={book} />
        ))}
      </ul>
    </div>
  );
};