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

redux-toolkit-generics

v1.0.6

Published

Sure thing! A well-crafted README file is crucial for an open-source library, especially on NPM, as it helps other developers understand the purpose, setup, and usage of your library. Here’s a guide for creating a detailed and effective README file for yo

Downloads

470

Readme

Sure thing! A well-crafted README file is crucial for an open-source library, especially on NPM, as it helps other developers understand the purpose, setup, and usage of your library. Here’s a guide for creating a detailed and effective README file for your generic Redux Toolkit library.

README File Template

Here is a comprehensive README file for your NPM package:


Redux Toolkit Generic Slice Library

A generic utility to easily create Redux slices with Redux Toolkit using TypeScript, enabling quick and reusable state management without the repetitive boilerplate code.

Table of Contents

Features

  • TypeScript Support: Fully typed, offering a great developer experience.
  • Simplifies Boilerplate: Reduces repetitive code required to manage state using Redux Toolkit.
  • Generic Solution: Flexible and reusable, allowing you to quickly generate Redux slices for multiple entities.

Installation

To use this library in your project, install it via npm:

npm install redux-toolkit-generics

If you're using Yarn:

yarn add redux-toolkit-generics

Getting Started

The library provides a generic function, createGenericSlice, that helps you create Redux slices with minimal boilerplate.

Usage

  1. Import the createGenericSlice function from the library.
  2. Pass in the entity name and a function for fetching data.

API

createGenericSlice<T>(name: string, fetchFunction: () => Promise<T[]>): { reducer, actions }

  • name: The name of the slice (string).
  • fetchFunction: An async function that returns an array of items (T[]) to populate your state.

Returns

  • reducer: The reducer function to be added to your store.
  • actions: An object containing the actions generated for the slice, including the async thunk.

Examples

Creating a User Slice

import { createGenericSlice } from 'redux-toolkit-generics';

// Define a function to fetch user data
const fetchUsers = async () => {
  return await fetch('/api/users').then((res) => res.json());
};

// Create a user slice
export const userSlice = createGenericSlice('user', fetchUsers);

// Export the reducer and actions
export const userReducer = userSlice.reducer;
export const userActions = userSlice.actions;

Creating a Post Slice

import { createGenericSlice } from 'redux-toolkit-generics';

// Define a function to fetch post data
const fetchPosts = async () => {
  return await fetch('/api/posts').then((res) => res.json());
};

// Create a post slice
export const postSlice = createGenericSlice('post', fetchPosts);

// Export the reducer and actions
export const postReducer = postSlice.reducer;
export const postActions = postSlice.actions;

Setup with Redux Store

After creating the slice, you need to add the reducer to your store:

import { configureStore } from '@reduxjs/toolkit';
import { userReducer } from './userSlice';
import { postReducer } from './postSlice';

export const store = configureStore({
  reducer: {
    users: userReducer,
    posts: postReducer,
  },
});

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

Demo

git clone https://github.com/mohamedma872/redux-toolkit-generics-Demo.git

Contributing

Contributions are welcome! If you find a bug, have an idea for improvement, or want to help expand the functionality, please feel free to submit a pull request or open an issue on GitHub.

Running Locally for Development

To set up the library locally:

  1. Clone the repository
    git clone https://github.com/mohamedma872/redux-toolkit-generics.git
  2. Install dependencies
    npm install
  3. Run the build script
    npm run build

License

This library is licensed under the MIT License. See the LICENSE file for more details.