question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getResource and RECEIVE reducer assumes a wrapped response

See original GitHub issue

Hey,

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:closed
  • Created 5 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mlabrumcommented, Apr 2, 2018

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:

Hey @mlabrum https://github.com/mlabrum, have you had any feedback from your team concerning this issue?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/Brigad/redux-rest-easy/issues/41#issuecomment-377896342, or mute the thread https://github.com/notifications/unsubscribe-auth/AASEqvnLD_I-CsH7Ex6R88BKYzcDl7xUks5tkfMggaJpZM4S2TPT .

1reaction
mlabrumcommented, Mar 22, 2018

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:

Hey, it was a choice to not include default normalizers, because most of the time the defaults would be wrong (sometimes the answer is wrapped in users, or user if the request was a POST/PATCH, or not wrapped at all…). However, we wanted it to be clear how to use normalizers, and it might not be.

Documentation on createResource can be found here https://github.com/Brigad/redux-rest-easy/blob/master/docs/api/createResource.md#createresourceresourcename-optionsactions, and specific documentation on actions configuration can be found here https://github.com/Brigad/redux-rest-easy/blob/master/docs/api/createResource/actionsConfig.md. Could you please tell me how we could improve the documentation on this point? I think we need a new point of view on this one 😃 Thanks a lot!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Brigad/redux-rest-easy/issues/41#issuecomment-375218047, or mute the thread https://github.com/notifications/unsubscribe-auth/AASEqo1hQO8BbGo13Rz6cuBRVgOINprjks5tg2JjgaJpZM4S2TPT .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hadoop: The Definitive Guide, 3rd Edition [Book]
All have a get() and set() method for retrieving and storing the wrapped value. Table 4-7. Writable wrapper classes for Java primitives. Java...
Read more >
scala.io.Source Scala Example
Source object Banner { def show(out: PrintStream): Unit = { val resourceName = "banner.txt" if (getClass.getClassLoader.getResource(resourceName) != null) out.
Read more >
hive/HiveConf.java at master · apache/hive
Get a set containing configuration parameter names used by LLAP Server instances ... "negative, Hive will use this one as the max number...
Read more >
MapReduce CHANGES.txt
Mappers get rescheduled on node transition even after all reducers are completed. ... Ensure MapReduce JobHistory Daemon doens't assume HADOOP_YARN_HOME and ...
Read more >
React-admin - Upgrading to v4
If your app used custom reducers, then you need to create a store manually, and wrap the app with a Provider component. import...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found