brocoders / redux-async-connect

It allows you to request async data, store them in redux state and connect them to your react component.
645 stars 103 forks source link

Way to trigger load manually #32

Open krzysztofpniak opened 8 years ago

krzysztofpniak commented 8 years ago

What about scenario with the refresh button on the page that enables user to grab fresh data without clicking around navigation.

I have small idea how this can be achived. You can add reload function on the same level we have loading and loaded properties. That function should just be the proxy to call again the promise funcion provided in @asyncConnect.

sars commented 8 years ago

Hi @krzysztofpniak Thanks for suggestion, it's interesting idea, but i don't think putting the function to the store is a good idea. Store should be serialisable object... you can achieve this manually reloading the data and dispatching loadSuccess action (from redux-async-connect) ...

krzysztofpniak commented 8 years ago

Hello @sars my idea wasn't about putting it to state, but to put it into object that is connected to react component only.

quarryman commented 8 years ago

Hi, @krzysztofpniak loaded and loading properties set by AsyncConnect are stored in store and connected to component.

Given that func you want should not be stored in store, AsyncConnect has nothing to do with it, as it connects store data only.

So if you want to extend connected data in your component, you should do it yourself. As @sars mentioned, you can import and dispatch loadSuccess action to reload AsyncConect data in store.

krzysztofpniak commented 8 years ago

Hello @quarryman , thanks for the explaination. I'm relatively new to Redux, so I might be wrong, but from my perspective there is nothing wrong with creating functions from the store and connect them to component. On the other hand I can say that asyncConnect should not contain its own reducer which adds loaded and loading, it should just dispatch load function. I hope you do not get me wrong. I'm just wondering why putting functions are bad, the state is not modified, store is serializable and components are stateless.

quarryman commented 8 years ago

@krzysztofpniak, basically js function is serializable, however it still might reference current scope variables and if you put func into state and try to use it later how can you guarantee func will be invoked within the same scope? Still it's controversial Basically Redux tells us that state must be a plain object. Any functions that aim to modify state should be implemented as reducer. That's the idea of react.

krzysztofpniak commented 8 years ago

@quarryman, which part of my answer was telling about putting a function to a state or about modifying a state from that function?

quarryman commented 8 years ago

Can you plz clarify your statement ?

but to put it into object that is connected to react component only.

What is connected can be state property or action creator. Did you mean function that is action creator?

kjanoudi commented 8 years ago

@krzysztofpniak @quarryman @sars I'm wondering about a similar approach here: https://github.com/Rezonans/redux-async-connect/issues/41

If I move my logic from an action dispatcher and a reducer to use solely the asyncConnect decorator, I have successfully gotten rid of two unnecesssary redundancies. However, if i cannot reload the data manually, then i still would need to invoke a dispatch of a new redux action, wait for the success action, then set data again. That seems redundant to me, given that if asyncConnect is handling the loading on the first load/route change, it should be able to handle it on manual invocation.

What about exposing an API call that would simply dispatch a LOAD again? The same one that is in the asyncConnect decorator already, we wouldn't need to serialize anything at that point. Just allow for the LOAD to be dispatched manually, and handle the success/failure calls the same way they're being handled now.

sars commented 8 years ago

Hi @kjanoudi , @krzysztofpniak , please, see my comment in #41