Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fields are mutable? #47

Closed
Kosta-Github opened this issue Feb 21, 2017 · 2 comments
Closed

fields are mutable? #47

Kosta-Github opened this issue Feb 21, 2017 · 2 comments

Comments

@Kosta-Github
Copy link

based on the docs my understanding is that fields of a model cannot be directly mutated, but can observe :-) the following behavior:

const Test = mobxStateTree.createFactory({
    field: 'hello',
    change: mobxStateTree.action(function(f) { this.field = f; })
});

const test = Test();
mobx.autorun(() => console.log(`field: ${test.field}`));

test.change('world');
test.field = 'universe'; // should this work?

// prints:
// field: hello
// field: world
// field: universe        <== ups?
@mattiamanzati
Copy link
Contributor

Yes, it should work indeed.
If you want that behaviour, you should

import {useStrict} from 'mobx'
useStrict(true)

when booting your project!

@mweststrate
Copy link
Member

There is now something better! (Once #68) is merged; instead of the global strict, you can just call protect(test), which makes it impossible to modify test without one of it's methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants