Code Monkey home page Code Monkey logo

Comments (2)

mweststrate avatar mweststrate commented on July 16, 2024

Good question, I'm gonna blog about this soon, so here is the TL;DR. We used to use setState indeed for local component state but discovered that it gives more issues than using @observable.

Too name a few:

  1. setState is asynchronous. If you call setState and after that call some other functions, they are still looking at the old state. I have seen leading this to very subtle things in the past
  2. setState makes it appear that all component state is captured in the state field. But in practice this doesn't uphold always, as any setState call triggers a re-render, while sometimes there is local state that doesn't influence rendering,

Using @observable fixes both this issues and has the nice upside that the component code becomes a lot more concises this.a = b instead of this.setState({ a: b }). And since local component state is now uniform with how you would use stores in MobX, it becomes way easier to factor local component state out when needed.

from mobx-contacts-list.

brikou avatar brikou commented on July 16, 2024

It makes sense and I would add (as a 3rd point) that doing so also ease debugging (with mobx 2.2 and the new @action decorator).

Thank you for your quick answer and your great lib.

from mobx-contacts-list.

Related Issues (4)

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.