Giter VIP home page Giter VIP logo

Comments (8)

solkimicreb avatar solkimicreb commented on May 28, 2024 3

Hi!

@mrjjwirght started working on this too. See this repo for more. Just wanted to let you know in case you are still interested 🙂

from observer-util.

SukantGujar avatar SukantGujar commented on May 28, 2024 2

Added a very quick and dirty example integration of nx-observe with React.

There are few things about integrating React with libraries like mobx and now nx-observe that are conceptually against some core React principles.

The first principle is to never update props directly. A React component should not mutate its props data as its an input to the component from parent. Popular implementations like redux, reflux etc. have well defined reducers or action handlers which take care of mutation. However with an observable store, every component can directly observe only those props its interested in and has the potential to make modifications to it. In fact as the data is mutable, it should make the changes. I would recommend passing an observable Store with mutation APIs which the components can invoke, rather than directly mutating the json. I will rework the code to give an example.

The second principle is to not invoke render directly. Ideally React manages all render invocations itself. However, both Mobx and nx-observe must allow easy updates to the components observing the store. To do this, Mobx provides a higher order component called observer which internally invokes render of the wrapped component once so that it can track the references observables in it. It is effective, but I somehow find this approach not very elegant (but hey the library is very popular and fast so they must have done this with a lot of thought). I think higher order components can be one way to solve this without invoking render directly. In the code, I have attempted a simple HOC observercomponent which does just this.

from observer-util.

kamicane avatar kamicane commented on May 28, 2024 1

I have been using a very barebones Observer component that uses the same technique of initial render patch as mobx:

https://gist.github.com/kamicane/e6a434ff12e623978aba4d0d3d5fab33

I find it cleaner to subclass Component rather than monkey-patch components using decorators.

from observer-util.

solkimicreb avatar solkimicreb commented on May 28, 2024 1

@kamicane @SukantGujar I made a react library that wraps the state into an observable and the render method into an observer (the whole thing is a single decorator). In case you are interested, it is here: https://github.com/solkimicreb/react-easy-state.

@kamicane Thanks for the gist, it was super helpful!

from observer-util.

solkimicreb avatar solkimicreb commented on May 28, 2024

Hi!

Nice work! (: Thanks for all the insights about React, it really helps. I run through the code quickly but didn't try it out yet. I will check it soon and comment on the nx-observe related parts where I can. Would you be interested in taking this further into an official port?

from observer-util.

SukantGujar avatar SukantGujar commented on May 28, 2024

I would be glad to do that, if you could bear with my timelines. At the moment my work is at full swing, leaving time for little else.

I'd use some help myself regarding more elegant ways to work around the two problems I mentioned above :).

from observer-util.

solkimicreb avatar solkimicreb commented on May 28, 2024

Sure, no need to hurry. I checked the app and its great. I think mobx react uses a mixin to add observability and only wraps stateless comps with a HOC (plus adds the mixin too). About the props I am clueless, I also feel like they are awkwardly 'in the way', but I guess mobx developers felt the same way.

Some observe related stuff: Things like observer.observe(func.bind(context, arg1, arg2)) can be rewritten as observer.observe(func, context, arg1, arg2). I decided to provide this to avoid binding and closures in perf sensitive code. (This is just for the future)

I am also a bit busy now, but I will look a bit deeper into mobx-react.

from observer-util.

solkimicreb avatar solkimicreb commented on May 28, 2024

Super clean!

from observer-util.

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.