Skip to content

How to use default constructor for nested model #1908

Answered by EmilTholin
davetapley asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @davetapley!

As the Miscellaneous Tips part of the documenatation states types.array(Thing) is the same thing as types.optional(types.array(Thing), []), but that's not the case for types.model.

You have to explicitly set it to types.optional(Store, {}) for it to work the way you would like:

const Thing = types.model({ idx: types.identifier });
const Store = types.model({ things: types.array(Thing) });
const Root = types.model({ thingStore: types.optional(Store, {}) });

const root = Root.create();

// Success!

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@davetapley
Comment options

Answer selected by davetapley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants