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

zustand-slice-factory

v1.0.6

Published

A light-weight package with generic factory functions for common slice data structures

Downloads

10

Readme

zustand-slice-factory 🐻🍕🏭

A light-weight package with generic factory functions for common slice data structures

Inspired by redux-slice-factory 🍕🏭

version types types types types

Install 💾

npm install zustand-slice-factory

What's the goal? 🏆

This package will

  • provide a more opinionated zustand experience
  • provide build-in support for async workflow
  • save you time & effort

What's a Slice? 🍕

A slice is a bundle of everything associated to a piece of zustand state { name, selectors, state, actions }

the name is the domain the slice is responsible for the selectors exposes functions to read data for state the state is the initial state used in the create method from zustand the action actions to be store along with state

Why use Slices?

Read this if you want to learn more

What's Included?

  • createModelSlice() create a slice for a single synchronous model
  • createEntitySlice() create a slice for a collection of synchronous entities
  • createAsyncModelSlice() create a slice for a single asynchronous model with request information
  • createAsyncEntitySlice() create a slice for a collection of asynchronous entities with request information

Why differentiate between async/sync slices?

  • Not every slice needs to track request information (i.e. UI data like Menus, Alert etc.)
  • Reduce amount of unused information in the store
  • SyncSlice can be easily extends into AsyncSlice if need be.

Show Me the Code...

Zustand (with Hooks)


type UserProfileSliceActions = AsyncModelSliceStateActions<UserProfileModel> & {
  get: Action<[string], Promise<void>>;
};

type UserProfileModelState = AsyncModelSliceState<UserProfileModel, Error, UserProfileSliceActions>;

const slice = createAsyncModelSlice<AppState, UserProfileModel, Error, UserProfileSliceActions>({
  name: 'UserProfile',
  selectSliceState: (appState) => appState.UserProfile,
});

// Create your own actions
const get: SetAction<AppState, [string], Promise<void>> = (set) => async (id) => {
  slice.actions.setStatus(set)(StatusEnum.Requesting);
  slice.actions.setError(set)(null);
  try {
    const response = await getUser(id); 
    slice.actions.hydrate(set)(response);
    slice.actions.setStatus(set)(StatusEnum.Settled);
  } catch (error) {
    slice.actions.setStatus(set)(StatusEnum.Failed);
    slice.actions.setError(set)(error as Error);
  }
};

const allActions = {
  ...slice.actions,
  get,
};

// Create final slice state that will be saved to the store
const createUserProfileSlice = (set: SetState<AppState>) => createAsyncModelSliceState<AppState, UserProfileModel, Error, UserProfileSliceActions>(
  set,
  slice.state,
  allActions
);

const UserProfileDuck = {
  create: createUserProfileSlice,
  name: slice.name,
  selectors: slice.selectors,
};

type AppState = {
  UserProfile: AsyncModelSliceState<UserProfileModel, Error, UserProfileSliceActions>
};

const useStore = create<AppState>((set) => ({
  UserProfile: UserProfileDuck.create(set),
}));

const UserProfile: React.FC = () => {
   const userActions = useStore(slice.selectors.selectActions);
   const userModelState = useStore(slice.selectors.selectStateSlice);

   useEffect(() => {
      userActions.set({
         username: '[email protected]',
         url: 'https://github.com/kevineaton603',
         roles: [UserRoleEnum.Premium],
      })
   }, [])
   return(<div>{userModelState.model.username}</div>)
}

Zustand Vanilla (without Hooks)

...

const useStore = create<AppState>((set) => ({
  UserProfile: UserProfileDuck.create(set),
}));

const { UserProfile } = store.getState();
UserProfile.actions.set({
   username: '[email protected]',
   url: 'https://github.com/kevineaton603',
   roles: [UserRoleEnum.Premium],
});