goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 322 forks source link

A confusion I have #675

Closed sjatkins closed 8 years ago

sjatkins commented 8 years ago

I am curious how often getPropsFromStores is actually called as stores update. Is it supposed to be called each time a store in the relevant stores changes? That is what I would hope but I am not terribly sure it is the intent. The reason I ask is that with a break point in that function some changes do not appear to cause the breakpoint to fire. Is there possibly a timing issue? Which raises the other question of what the current best practice is to make a possibly asynchronous call to initialize a store. I have seen suggestions with bootstrap but I am unsure where exactly that should be used. With the call being async is there a possible timing issue? Should await be used?

And lastly, is there some other forum rather than issues to go to for alt questions?

jdlehman commented 8 years ago

getPropsFromStores is called when the component is first mounted as well as any time the stores it is listening to are updated. So if you want to trigger some state whenever a breakpoint changes you are probably better off having a media query listener somewhere in your app that fires an action and updates data in the store to reflect the breakpoint. If only one component is concerned with the breakpoint change you can put the listener in componentDidMount and set local component state to store this data.

Alternatively (and probably more easily) you can use the react-responsive or react-media libraries to simplify using breakpoints in your app.

Regarding asynchronously initializing your stores, you can use bootstrap in the callback to your async call, or the promise, depending on what library you are using to make ajax calls.

// promise example
getDataFromServer().then(function(data) {
  alt.bootstrap(JSON.stringify(data));
});

// callback example
getDataFromServer(function(data) {
  alt.bootstrap(JSON.stringify(data));
});

regarding a forum for questions, you can join the gitter channel

goatslacker commented 8 years ago

There's also a method componentDidConnect which goes where getPropsFromStores lives. You can setup any async in there and that method will be called after the store is connected to the component.

You might want to use this if you're having a weird race condition of store updates before it's connected.