Comments (10)
@goatslacker Any preference on what the end-user API would be? Something like...?
mixins: [
alt.mixins.asyncProp('myResult', MyAction.fetchResult),
]
from alt.
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.
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.
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.
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.
@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.
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.
@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.
Opened a ticket on that: ericclemmons/react-resolver#14
from alt.
Deferring to #49
from alt.
Related Issues (20)
- Is this still alive? HOT 2
- sequentially perform actions that require source retrieval at same time HOT 2
- Error handler does not get triggered for ajax calls HOT 4
- Wait for store function to complete
- Different tests competing in modifying the store
- Multiple input in controlled forms
- Public Method returns state not store; am I guaranteed the Store is updated when the listener fires? HOT 1
- PropTypes in React 16 Deprecated HOT 2
- dispatchIdentity rudely omit all params when the first one is undefined.
- Upgrade to Webpack 3 HOT 1
- Store not listening to actions HOT 4
- can not call DataSource function during an action
- Typescript does not work
- Using bindListeners vs Store.listen emits changes several seconds slower HOT 1
- docs specify file that does not exists
- How to test async actions
- ss state in compoennts HOT 2
- Trato de hacer la mostrar mis registros en paginas HOT 1
- Documentation needs to be fixed HOT 4
- Documentation needs to be fixed
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 alt.