Comments (11)
I found the reason:
"Make sure that an initial value is assigned to all state properties before calling makeAutoObservable(), otherwise the property will not be observable. This is a MobX limitation since we can’t use the TypeScript configuration "useDefineForClassFields": true due to conflict with Lit decorators mentioned above."
For more information https://vaadin.com/docs/latest/fusion/application/state-management
Is it possible to add this to readme?
Thx and nice work!
from lit-mobx.
Apologies I missed both these comments somehow.
@justinfagnani do you have any more insight as to the right outcome here given the conflicting requirements of mobx and lit in this case?
from lit-mobx.
I think until standard decorators are available, or TypeScript adds the accessor
keyword, we have a few workarounds:
- Always assign an initial value to observable properties, even if
undefined
. This creates an instance property on the object that MobX can see and make observable. - Turn
useDefineForClassFields
on and usedeclare
for reactive fields. Example - Turn
useDefineForClassFields
on and usedeclare
for reactive fields and use thestatic properties
field instead of decorators.
from lit-mobx.
On one app we're on Mobx5, but on the other we're on Mobx6 and have not specified a value for useDefineForClassFields
, though I suspect we have always initialized properties with a default value.
from lit-mobx.
Agreed the readme should probably mention that useDefineForClassFields
must be set to false
for Lit.
However, this conflicts with MobX's requirement that useDefineForClassFields
be set to true
. You can still make MobX work with it set to false
, but you need to be more careful to always assign values to your observable properties (if you don't assign an initial value, the property will not be observable). And as far as I can tell, you can't use decorator syntax with MobX if Edit: decorators still work either way.useDefineForClassFields
is `false.
There's a thread here from a member of the Lit team asking MobX about this:
mobxjs/mobx#3216
from lit-mobx.
1 seems the simpler thing to suggest to consumers of the library?
So probably an update to the README to indicate why this shows up (due to the mismatch in useDefineForClassFields
between MobX and Lit decorators. I guess the recommendation is to keep useDefineForClassFields
to false
so that lit works, and then just recommend our users always assign an initial value for observable properties then?
from lit-mobx.
Thanks for all the info to everyone... I'll use option 2 personally as I'm not using reactive fields (or rarely) That's probably the decision point — if you want to use MobX decorators, you have to use Edit: part about decorators wasn't accurate.useDefineForClassFields
set to true
(as far as I can tell in testing).
from lit-mobx.
This is whats confusing me, we're using both Mobx and Lit decorators at Adobe right now with this, so I'm unsure what we did to support that :-)
from lit-mobx.
Oh interesting, what's your useDefineForClassFields
setting? And are you on MobX 6? I'll keep toying around to see if I can figure out more as well.
from lit-mobx.
Ok I need to walk back the bit about MobX decorators — I created a simple repro from scratch and with useDefineForClassFields
set to false
, MobX decorators still work as long as you assign an initial value to properties. Must be something else in my more complicated app that's breaking with useDefineForClassFields
set to false
. Editing my previous comments.
So my only observed drawback to the false
config with MobX is that you must assign initial values to fields or they aren't made observable.
from lit-mobx.
Thanks for hte update, closing this out then as not reproducible.
from lit-mobx.
Related Issues (20)
- Demo isn't working, state change not causing a re-render HOT 7
- updated not called if observable not used in render HOT 5
- An in-range update of es-dev-server is breaking the build 🚨 HOT 2
- Class instance should be referred to as a singleton HOT 2
- An in-range update of @open-wc/testing-karma is breaking the build 🚨 HOT 2
- No tests were run as of today HOT 1
- An in-range update of @open-wc/testing is breaking the build 🚨 HOT 7
- An in-range update of @open-wc/testing-karma is breaking the build 🚨 HOT 11
- non-typescript examples
- Updates to observables in `firstUpdated` not working HOT 9
- An in-range update of es-dev-server is breaking the build 🚨 HOT 15
- An in-range update of @open-wc/testing-karma is breaking the build 🚨 HOT 16
- Add release tags to make it easier to see changes between releases HOT 3
- Move to v1.0.0 HOT 4
- Does not work with MobX 6.0.0+ HOT 7
- Lit-Mobx and partial rerendering HOT 2
- Mobx6 example does not work with Node 15 / npm 7.3 HOT 2
- Batch updates and lit-mobx HOT 1
- Lit 2 version HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lit-mobx.