Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
zustand-slice-factory
Advanced tools
Readme
A light-weight package with generic factory functions for common slice data structures
Inspired by redux-slice-factory 🍕🏭
npm install zustand-slice-factory
This package will
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
Read this if you want to learn more
createModelSlice()
create a slice for a single synchronous modelcreateEntitySlice()
create a slice for a collection of synchronous entitiescreateAsyncModelSlice()
create a slice for a single asynchronous model with request informationcreateAsyncEntitySlice()
create a slice for a collection of asynchronous entities with request information
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>)
}
...
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],
});
FAQs
A light-weight package with generic factory functions for common slice data structures
The npm package zustand-slice-factory receives a total of 5 weekly downloads. As such, zustand-slice-factory popularity was classified as not popular.
We found that zustand-slice-factory demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.