Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About caching and reusing data in stores #180

Closed
gpbl opened this issue Jan 5, 2015 · 5 comments
Closed

About caching and reusing data in stores #180

gpbl opened this issue Jan 5, 2015 · 5 comments
Labels

Comments

@gpbl
Copy link

gpbl commented Jan 5, 2015

Now that v0.2.2 provides a nicer API for loading asynchronous data, I want to place my API requests in actions. Yet I'm not sure how to reuse data already cached in the stores.

(I'm sorry if you have already discussed this thing before, maybe I've lost the discussion somewhere. Also, I miss a gitter chat :-)

Before, i had my stores managing this:

// store listening to `load` action
onLoad() {
   if (this.getData()) 
     // use cached data    
     this.trigger(this.getData())
   else 
     // async bunga bunga
}

I was tempted to do something like this:

// actions
myActions.load.listen(() => {
    if (myStore.getData()) 
       // use cached data
       myActions.load.completed(myStore.getData())
    else 
      // start async operation ...
});

but this won't work, since actions is required from store as well, causing a circular dependency.

I solved putting the logic in the component:

// mycomponent.jsx

componentDidMount() {
   if (store.getData()) 
      // reuse cache
      this.setState({data: getData()})
   else
      // async action
      actions.loadData()  
}

I prefer this approach because:
– a possible loading state is managed from the component and not the store
– my getData() function could receive an argument with the fields I need from my data (in case of the data is available but incomplete)

Does this sound right or have you found a better approach?

@gpbl
Copy link
Author

gpbl commented Jan 5, 2015

I've tried to put my reasonings in this gist. The case is related to Combine results React getInitialState and Reflux getInitialState #166 and may be a case for server-rendering #144? (I know @appsforartists has made progress but i'm still some steps behind :-) )

@spoike spoike added the question label Jan 7, 2015
@simonsmith
Copy link

@gpbl I use sessionStorage on this small app - https://github.com/simonsmith/github-user-search

I decided to allow the actions to read from the cache and let the stores save to the cache. When calling the this.completed method from the action I pass along whether it was from the cache or not, then the stores know whether to do something with the data.

Hope that helps

@damassi
Copy link

damassi commented Feb 11, 2015

I'm quite curious about the preferred approach here as well.

@ivan-kleshnin
Copy link
Contributor

I personally think, that only store should be concerned with data retrieval.
Check it's state, nope, check localStorage, nope then raise ajax request.
The approach when you shift data-related stuff to actions or scatter cache setters and getters through application parts smells bad to me.

@spoike
Copy link
Member

spoike commented Jul 11, 2015

In order to clean up the issue tracker I have to close down all questions. Please use proper communications channels for questions about reflux, such as:

  1. StackOverflow with the refluxjs tag
  2. #reflux channel on Reactiflux Slack group. Sign up here for an account.
  3. Gitter
  4. Thinkful

@spoike spoike closed this as completed Jul 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants