Giter Club home page Giter Club logo

Comments (11)

knuspertante avatar knuspertante commented on May 29, 2024 1

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.

benjamind avatar benjamind commented on May 29, 2024 1

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.

justinfagnani avatar justinfagnani commented on May 29, 2024 1

I think until standard decorators are available, or TypeScript adds the accessor keyword, we have a few workarounds:

  1. 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.
  2. Turn useDefineForClassFields on and use declare for reactive fields. Example
  3. Turn useDefineForClassFields on and use declare for reactive fields and use the static properties field instead of decorators.

from lit-mobx.

benjamind avatar benjamind commented on May 29, 2024 1

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.

StephenHaney avatar StephenHaney commented on May 29, 2024

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 useDefineForClassFields is `false. Edit: decorators still work either way.

There's a thread here from a member of the Lit team asking MobX about this:
mobxjs/mobx#3216

from lit-mobx.

benjamind avatar benjamind commented on May 29, 2024

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.

StephenHaney avatar StephenHaney commented on May 29, 2024

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 useDefineForClassFields set to true (as far as I can tell in testing). Edit: part about decorators wasn't accurate.

from lit-mobx.

benjamind avatar benjamind commented on May 29, 2024

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.

StephenHaney avatar StephenHaney commented on May 29, 2024

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.

StephenHaney avatar StephenHaney commented on May 29, 2024

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.

benjamind avatar benjamind commented on May 29, 2024

Thanks for hte update, closing this out then as not reproducible.

from lit-mobx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.