A generic utility to easily create Redux slices with Redux Toolkit using TypeScript, enabling quick and reusable state management without the repetitive boilerplate code.
- 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.
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
The library provides a generic function, createGenericSlice
, that helps you create Redux slices with minimal boilerplate.
- Import the
createGenericSlice
function from the library. - Pass in the entity name and a function for fetching data.
name
: The name of the slice (string).fetchFunction
: An async function that returns an array of items (T[]
) to populate your state.
reducer
: The reducer function to be added to your store.actions
: An object containing the actions generated for the slice, including the async thunk.
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;
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;
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;
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.
To set up the library locally:
- Clone the repository
git clone https://github.com/mohamedma872/redux-toolkit-generics.git
- Install dependencies
npm install
- Run the build script
npm run build
This library is licensed under the MIT License. See the LICENSE file for more details.