Connect
The data-fetching decorator for Lore
The data-fetching decorator for Lore
This blueprint allows you to extract a model from the store by it's id
attribute. If the model doesn't exist, the get
action will be invoked to retrieve it.
Basic usage is below:
import { connect } from 'lore-hook-connect';
connect((getState, props) => {
return {
tweet: getState('tweet.byId', {
id: 1,
query: {
_embed: 'user'
}
})
}
})
There are two attributes you can provide in the params
object.
The first attribute is id
, which is required, and is the id
(or primary key) of the resource you want to retrieve. It can be a string
or a number
.
The second attribute is query
, which is an object of query parameters you want sent to the API.
The example about would become the API call /tweets/1?_embed=user
, which, if you're usingjson-server
, would nest the user resource inside the tweet.
export default {
defaults: {
id: null
},
verifyParams: function(params) {
if (!params.id) {
throw new InvalidGetStateCall(this.reducerKey);
}
},
getPayload: function(reducerState, params) {
const key = params.id;
return reducerState[key];
},
callAction: function(action, params) {
const id = params.id;
const query = params.query;
return action(id, query).payload;
}
};