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-enhanced-slice

v1.0.1

Published

A utility function for creating enhanced Redux slices with standardized cases and selectors.

Downloads

6

Readme

🌜 Redux Enhanced Slice 🌛

npm

NPM

redux-enhanced-slice-enhanced-slice is an NPM package that simplifies the creation of Redux slices by providing standardized cases for handling asynchronous actions and generating selectors for accessing the slice state. 🎉

Features ✨

  • Simplifies slice creation: Easily create Redux slices with just a few lines of code.
  • Handles asynchronous actions: Provides standardized cases for handling asynchronous actions, such as loading, success, and error states.
  • Generates selectors: Automatically generates selector functions for accessing the slice state in a consistent manner.
  • Reduces boilerplate: Minimizes the need for repetitive code, making your Redux codebase more maintainable and easier to understand.

Motivation 🤔

Redux is a great tool for managing state in React applications, but it can be difficult to set up and maintain. redux-enhanced-slice aims to simplify the process of creating Redux slices by providing standardized cases for handling asynchronous actions and generating selectors for accessing the slice state. This reduces the amount of boilerplate code needed to create and maintain Redux slices, making your codebase more maintainable and easier to understand.

Installation 💾

To install the package, run the following command:

npm install redux-enhanced-slice

Usage 📚

To create a Redux slice with redux-enhanced-slice, simply import the package and use the createEnhancedSlice function. This function accepts the slice name and initial state as arguments and returns an object containing the created slice, queryTypes, and selectors.

Example 📖

import createEnhancedSlice from 'redux-enhanced-slice';

const { slice, queryTypes, selectors, reducer, actions } = createEnhancedSlice('users', {
  users: [],
  user: null,
}, {
  reducers: {
    setUsers: (state, action) => {
      state.queryTypes.users = action.payload;
    },
    setUser: (state, action) => {
      state.queryTypes.user = action.payload;
    },
  },
});

const { 
  fetchUsers, 
  fetchUser, 
  setPageData // Internal action for setting page data
} = slice.actions;

const { selectUsersUsers, selectUsersUser } = selectors; // pre-generated selectors for accessing state
const { setUsers, setUser } = actions; // pre-generated action creators for dispatching actions
const { users, user } = queryTypes; // pre-generated query types for accessing state types in selectors

export default reducer;

Behind the scenes the users reducers looks a bit like this, with the selectUsersUser and selectUsersUser reducers being automatically generated:

 'usersUser:211951': {
  results: [],
  isLoading: false,
  hasMore: true,
  page: 0,
  errors: null
},
'userUser:211951': {
  results: null,
  isLoading: false,
  hasMore: true,
  page: 0,
  errors: null
}

In this example, we create a users slice with an initial state containing users and user. The createEnhancedSlice function returns the slice, queryTypes, and selectors for accessing the state. The cases object defines the asynchronous actions associated with the slice.

To ignore the pregenerated reducers, set ignoreDefaultReducers to true in the options object:


const { slice, queryTypes, selectors, reducer, actions } = createEnhancedSlice('users', {
  users: [],
  user: null,
}, {
  reducers: {
    setUsers: (state, action) => {
      state.queryTypes.users = action.payload;
    },
    setUser: (state, action) => {
      state.queryTypes.user = action.payload;
    },
  },
}, {
  debug: true, // Logs the generated selectors and actions to the console
  ignoreDefaultReducers: true // When set to true, the default reducers will not be generated NB this disables the setPageData function and can cause issues with the slice
});

Alternatively, you can use the extraReducers object to add additional reducers to the slice:

  
const { slice, queryTypes, selectors, reducer, actions } = createEnhancedSlice('users', {
  users: [],
  user: null,
}, {
  reducers: {
    setUsers: (state, action) => {
      state.queryTypes.users = action.payload;
    },
    setUser: (state, action) => {
      state.queryTypes.user = action.payload;
    },
  },
}, {
  extraReducers: {
    [fetchUsers.pending]: (state, action) => {
      state.queryTypes.users.isLoading = true;
    },
    [fetchUsers.fulfilled]: (state, action) => {
      state.queryTypes.users.isLoading = false;
      state.queryTypes.users.results = action.payload;
    },
    [fetchUsers.rejected]: (state, action) => {
      state.queryTypes.users.isLoading = false;
      state.queryTypes.users.errors = action.payload;
    },
    [fetchUser.pending]: (state, action) => {
      state.queryTypes.user.isLoading = true;
    },
    [fetchUser.fulfilled]: (state, action) => {
      state.queryTypes.user.isLoading = false;
      state.queryTypes.user.results = action.payload;
    },
    [fetchUser.rejected]: (state, action) => {
      state.queryTypes.user.isLoading = false;
      state.queryTypes.user.errors = action.payload;
    },
  }
});

API Reference 📑

createEnhancedSlice

The createEnhancedSlice function creates a Redux slice with the given name and initial state.

Parameters

  • name (string): The name of the slice, used to identify it in the Redux store.
  • initialState (object): The initial state of the slice.
  • cases (object): An object containing the asynchronous actions associated with the slice.
  • reducers (object): An object containing additional reducers for the slice.
  • extraReducers (object): An object containing extra reducers for the slice.
  • options (object): An object containing options for the slice.
    • HYDRATE (boolean): Whether to include a HYDRATE action in the slice. Defaults to ''.
    • debug (boolean): Whether to enable debug mode. Defaults to false.
    • ignoreDefaultReducers (boolean): Whether to ignore the default reducers. Defaults to false.

Returns

An object containing the created slice, queryTypes, and selectors:

  • slice (object): The created Redux slice.
  • queryTypes (object): Maps the initial state keys to string values.
  • selectors (object): Contains selector functions for each key in the queryTypes object.
  • reducer (function): The reducer function for the slice.
  • actions (object): Contains action creators for each key in the queryTypes object.

License 📄

MIT License