Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

License

NotificationsYou must be signed in to change notification settings

qontu/component-store

Repository files navigation

Inspired (semi-forked) ofhttps://github.com/amcdnl/ngrx-actions

The mission of this (another more... 😆)store is to maintain the state of a component (not the wide app).

I have been searching for one and I didn't find any... then, I created it 😅

Dependencies

This module depends onimmer

How to install?

npm i immer @qontu/component-store oryarn add immer @qontu/component-store

How to use?

You can see an examplehere

Why do I need to install immer?

Because it's a powerful inmutable state tree, it allow us to do:

interfaceUserListState{users:User[],usersMap:Record<number,User>,}@Store<UserListState>({users:[],usersMap:{},})exportclassUserListStore{  @Action(Update)updateUsingImmer(state:UserListState,{ user}:Update){constuserToUpdate=state.users.find(({ id})=>id===user.id);Object.assign(userToUpdate,user);}  @Action(Update)updateWithoutImmer(state:UserListState,{ user}:Update){return{      ...state,users:[        ...state.users.filter(({ id})=>id!==user.id),user,]}}// Update an object-map  @Action(Update)updateUsingImmer(state:UserListState,{ user}:Update){state.usersMap[user.id]=user;}  @Action(Update)updateWithoutImmer(state:UserListState,{ user}:Update){return{      ...state,usersMap:[        ...state.usersMap,[user.id]:user,]}}}

you can use both in the same time, choose the one that you prefer 😄


[8]ページ先頭

©2009-2025 Movatter.jp