GithubHelp home page GithubHelp logo

Comments (10)

ericclemmons avatar ericclemmons commented on June 28, 2024

@goatslacker Any preference on what the end-user API would be? Something like...?

mixins: [
  alt.mixins.asyncProp('myResult', MyAction.fetchResult),
]

from alt.

goatslacker avatar goatslacker commented on June 28, 2024

That's an interesting approach to do it on a per prop/action level. I was thinking of importing an entire store and all of the state gets added in. I don't have any specifics in mind, that's up to debate. Just something that makes sense.

👍 on that API though

from alt.

ericclemmons avatar ericclemmons commented on June 28, 2024

Not sure I 100% understand, except maybe if UserStore.getState() return { users: [...], current: { ... }, etc. }, and those were set on this.props for easy access.

Let me know what you have in mind...

from alt.

goatslacker avatar goatslacker commented on June 28, 2024

I don't have anything concrete in mind, just thinking out loud. What I said probably doesn't make sense and needs more thought, your approach is sound.

from alt.

mattmccray avatar mattmccray commented on June 28, 2024

Well, in my projects I use a StoreStateMixin that is used like this:

export var Example= React.createClass({
  mixins: [
    StoreStateMixin( StoreA, StoreB)
  ],

  getStoreState() {
    return {
       fromA: StoreA.getSomething(),
       fromB: StoreB.getSomethingElse()
    }
  }
})

The mixin automatically wires up the Store change events to call getStoreState(), setting the result into the Component's state. Obviously this doesn't take into account async data. Usually I'll make a ActionsA.fetchSomething() call in componentDidMount().

Perhaps something like this could work in statics?

var Example2= React.createClass({
  statics: {
    storesToProps: {

      stores: [StoreA, StoreB], // So change events can be wired up?

      fromA( assignData) { 
        assignData( StoreA.getSomething())  // Gets assigned to this.props.fromA
      },

      fromB( assignData) {
        // If you're using Promises in your action, you can consume them...
        ActionsB.fetchSomething().then(() => { 
          assignData( StoreB.getSomethingElse() )
        })
      }
    }
  }
})

I'm avoiding Promises in this example for multiple reasons including: No shimming required, and no error swallowing.

I'm just kind of "coding out loud" here to keep the discussion going. :)

Thoughts?

from alt.

troutowicz avatar troutowicz commented on June 28, 2024

@mattmccray That approach looks like it could work.

I'm curious though on how a mixin for data fetching would really be any different than just simply executing an action within componentDidMount(). What's the benefit? Aren't we defining data fetching at the component level when we call an action?

from alt.

goatslacker avatar goatslacker commented on June 28, 2024

There may not be much utility to this specially with something like react-resolver but I wanted to open this up for discussion

from alt.

mattmccray avatar mattmccray commented on June 28, 2024

@troutowicz It comes down to resolution of data before or after a component is mounted. With the statics approach, it could be tied to react-router and the component's mounting could be delaying until the required data is available. The mixin approach, which I currently use, doesn't try to fetch any data until the component has been mounted. In my case, I'm using a modified version of react-router's fetchData() in the few places I need to ensure there's data before a route is rendered.

As far as why I use a mixin instead of just wiring it up directly in the component's lifecycle events... Mainly to save me boilerplate. The app I'm using this in is a very large, mostly CRUD app. So there are tons of controller components.

I like the look of react-resolver, but unfortunately can't use it in my project -- it simply has too many dependencies to make a stand alone version (as is).

from alt.

goatslacker avatar goatslacker commented on June 28, 2024

Opened a ticket on that: ericclemmons/react-resolver#14

from alt.

goatslacker avatar goatslacker commented on June 28, 2024

Deferring to #49

from alt.

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.