Replies: 1 comment 8 replies
-
For your examples, you can still use createNewItem: assign(({ context, event }) => {
const new_item = {/* ... */}
const items = [...context.items, new_item]
return {
items,
currentSelectedItem: new_item
} // partially update context
}) And, for the second example, I'm going to take the opportunity to demonstrate how you can use Immer to help: import { produce } from 'immer';
// ...
updateItem: assign(({ context, event }) => {
return produce(context, draft => {
draft.items[ event.item_idx ][ event.item_property_to_update ] = event.new_value;
})
}) See Usage with Immer for more information. |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I understand there are ideological reasons behind making context immutable and only updatable from within actions.
I have managed to get the impression that the accepted way to update the context is with the assign() action. But I don't seem able to call assign() from within any other action - the action itself has to be "assign".
I immediately ran into multiple situations where you want to assign() from within a custom action. For example, you need to update context with information that you don't have until you calculate it, or you need to update a nested value but you don't know what it is yet.
Here's a simple example of something I want to do, which I think is a pretty commonplace requirement:
I obviously can't use the code above. So what is the right way to do this?
I am using xstate v5.
Beta Was this translation helpful? Give feedback.
All reactions