Search code examples
statestate-managementxstate

Nested property assign in xstate?


I know that you can use the assign action creator like this:

const setFoo = assign({
  foo: (context, event) => "someValueBasedOnContextAndEvent",
});

But can you use it like this?

const setFoo = assign({
  foo: {
    bar: (context, event) => "someValueBasedOnContextAndEvent",
  }
});

Of course, without deleting all other nested values of foo.


Solution

  • David Kourship (creator of xstate) answered this on Spectrum:

    No you cannot, but you can make a custom assign function that does that.

    So we basically have to create a custom reducer that selectively updates only one field in a nested object.