cantierecreativo / redux-bees

A nice, short and declarative way to interact with JSON APIs
592 stars 41 forks source link

Query using already fetched data from redux store #57

Open planetcrypton opened 6 years ago

planetcrypton commented 6 years ago

I my our setup we want the query HOC to fetch the data, save it in the redux store (which it obviously does), but reuse that data, when its component is mounted for a second time, and avoid that extra http call. What would be a good way of achieving this?

stueynet commented 6 years ago

We have a similar issue. We are using getRelationship as follows:

@query('message', api.getMessage, (perform, props) => (
    perform({ threadId: props.match.params.threadId, messageId: props.messageId, include: 'user' })
))

@connect((state, props) => {
    return (
        {
            user: props.message && getRelationship(state, props.message, 'user')
        }
    )
})

We would expect that once the related resources (users) are loaded, it should not require another api call at least for a specified amount of time. What we are finding is that each time we load the same message list, it makes all the individual user api calls as well for each message.

benjy commented 6 years ago

I started off working around this by having the data fetched in a parent component that isn't unmounted and then passed through as props, that doesn't very well at all though.

My current solution is to have query check if the request has started before calling fetch: https://github.com/cantierecreativo/redux-bees/pull/59

stueynet commented 6 years ago

Anybody home here? @benjy your PR seems to work well.