getResource and RECEIVE reducer assumes a wrapped response
See original GitHub issueHey,
I’ve followed the sample code and modified it to point to https://jsonplaceholder.typicode.com/users
which returns an array of users
Now running the following code
const users = createResource("users")({
retrieve: {
method: "GET",
url: "https://jsonplaceholder.typicode.com/users",
afterHook: () => console.log("Users retrieved successfully")
}
});
const Component = connect(
state => ({
isFetching: users.selectors.retrieve.resource.isPerforming(state),
data: users.selectors.resource.getResource(state),
}),
{
onRetrieveUsers: users.actions.retrieve.perform
}
)(({ isFetching, data, onRetrieveUsers }) => (
<div>
<button onClick={onRetrieveUsers}>Fetch</button>
<div>IsFetching: {JSON.stringify(isFetching)}</div>
<pre>data: {JSON.stringify(data, null, 2)}</pre>
</div>
));
// Mount component etc..
Will result in users.selectors.resource.getResource(state)
returning []
as it can’t find the users
key under state.restEasy.resources
as https://github.com/Brigad/redux-rest-easy/blob/master/src/internals/reducer/generateReducer/getReducerCases.js#L79 just merges the response into the state.restEasy.resources
which results in the following state
"resources": {
"0": {
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
... more users ....
},
Once I dug into the code I realised that I needed to add a normalizer to get the reducer placing the data in the correct location
const users = createResource("users")({
retrieve: {
method: "GET",
url: "https://jsonplaceholder.typicode.com/users",
afterHook: () => console.log("Users retrieved successfully"),
normalizer: result => ({
entities: { users: result },
result: null
})
}
});
then the state is correctly added under state.restEasy.resources.users
This means that for every resource I’ll need a normalizer to place it under the correct key. I would have assumed that by creating a resource that the reducer would automatically place the data in the correct location?
Could we update the default normalizer to automatically wrap it under the resource name? Perhaps this could make this a configuration option?
Thanks
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (3 by maintainers)
Top GitHub Comments
Hey,
Sorry I thought I would have time to organise this but I’m currently on holiday in new Zealand, returning on the 16th.
The feedback was pretty much to enforce defining a normalizer and for a common use case perhaps including a set of normalizers that could be imported.
I’ll write up a more detailed response when I return back to Australia 😃
On Mon, 2 Apr. 2018, 9:45 pm Adrien HARNAY, notifications@github.com wrote:
Thanks 😃 I’ll be running my team over this in the next few days and I’ll ask for suggestions. Perhaps making normalise required (even if it’s set to false) , as not having it can lead to odd bugs
On Thu, 22 Mar. 2018, 7:05 pm Adrien HARNAY, notifications@github.com wrote: