Socket
Socket
Sign inDemoInstall

zustand-slice-factory

Package Overview
Dependencies
6
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

zustand-slice-factory

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


Version published
Maintainers
1
Weekly downloads
5
increased by66.67%

Weekly downloads

Readme

Source

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: 'kevin.saco.eaton@gmail.edu',
         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: 'kevin.saco.eaton@gmail.edu',
   url: 'https://github.com/kevineaton603',
   roles: [UserRoleEnum.Premium],
});

Keywords

FAQs

Last updated on 08 Mar 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc