You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Iterating on store data arrays does not have a dedicated operator.
For dynamic data this is necessary where we don't have a target for updateItem.
Recursion with NGXS state is a very common question from users.
We should probably be using built-in operators over custom code in state files.
Describe the problem you are trying to solve
Various engineers have stated that there exists no NGXS operator to iterate an array.
This causes pain when updating nested data like the tasks arrays in the image above. I've had to write native JS for recursion and to update nested items.
Describe the solution you'd like
An iterate operator that allows us to run operators on each index of the data store.
This way we could execute an iif and subsequent operators (possible recursion) on each index.
ctx.setState(
patch({
board: patch({
// iterate statuses, look for the key 'tasks', run an operator on this index
statuses: iterate(
'tasks': iif(
(tasks) => tasks?.some((task: any) => task.task_id === payload.task_id), // condition
updateItem<any>((task: any) => task.task_id === payload.task_id, props) // if true
// insertItem() // if false
),
)
}),
})
);
Describe alternatives you've considered
Currently using native JS to update nested items.
Tried many variations of operators to make this happen and spoke to several members of the community.
Here is a Stackblitz with the problem all set up so solve (see @Action(UpdateTask))
This discussion was converted from issue #1918 on September 27, 2022 20:33.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Relevant Package
This feature request is for @ngxs/store/operators
Description
Iterating on store data arrays does not have a dedicated operator.
updateItem
.Describe the problem you are trying to solve
Various engineers have stated that there exists no NGXS operator to iterate an array.
This causes pain when updating nested data like the
tasks
arrays in the image above. I've had to write native JS for recursion and to update nested items.Describe the solution you'd like
An
iterate
operator that allows us to run operators on each index of the data store.This way we could execute an
iif
and subsequent operators (possible recursion) on each index.Describe alternatives you've considered
Currently using native JS to update nested items.
Tried many variations of operators to make this happen and spoke to several members of the community.
Here is a Stackblitz with the problem all set up so solve (see
@Action(UpdateTask)
)Beta Was this translation helpful? Give feedback.
All reactions