-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dataProvider reuses filter object between requests #8888
Comments
I don't understand the bug. Can you give an example and a reproduction? |
I'm running a little short on time, but imagine you have 2 different resources, which both have a ReferenceManyField with different targets (e.g. If you use the code from the first example (replacing dataRequest() with whatever you need to do a get_list request), then when you view the list for the first resource and look at the network request made, you'll see Then if you navigate to view the list of the second resource, you'll see requests made with This is because |
I still don't understand. Would you mind building a reproduction based on the simple example StackBlitz? https://stackblitz.com/github/marmelab/react-admin/tree/master/examples/simple |
I need to run out, and can't get the ReferencManyField working properly in time. But, you can still see the root of the problem here: If you look at the console logs when the page loads the posts view for the first time, you'll see the first getMany log has a filter of If you get a 2nd ReferenceManyField working on comments or something, then after loading the comments view you'd see a filter of |
I still don't understand what I'm supposed to look at. Please be more specific. Also, you should not modify the parameters passed as arguments to a function inside a function. Lots of react-admin code (and React code) is based on the assumption that you will not do that:
Otherwise, every function call should be done by value and not by reference, and we'd have to clone everything. |
That is the issue, I was just suggesting that it be documented if sending a new object in every call is not something you want to do (which sounds like that is the react way...).
Which is exactly the solution I've used, as shown in the original comment. |
Thanks for your reply. |
Maybe it's normal for react, but I think it has nothing to do with the way JavaScript works. In a typical application, I'd expect a call to look something like:
I'm not sure why how it actually works in react, but it seems like the behaviour is something more like:
As I say, I'm not familiar with React, but in other projects/languages that latter would be considered bad practice, and that's why this caught me out. Reusing the same mutable object for every request just seems unusual to me. If that's the React way, then feel free to leave it, but as a non-React developer I did not expect this behaviour. |
If you also use async code, I don't understand how this is meant to work correctly. e.g. If some code is awaiting inside a getMany() request, and a new, different, getMany() request is made, then the object in the first request is going to be changed and potentially break the code. (Probably, there are not multiple simultaneous calls to getMany() for most people, so maybe it never happens in reality). |
What I meant by "it's the way JavaScript works" was referring to passing the parameters by value vs. by reference, and the need to clone the parameters with the latter. |
Sure, but from that link:
What I provided was a pure function, it only modified the input given to it as an argument. The fact that a global, reusable object was passed to the function is the only thing I'm questioning (why pass the object as an argument if it's global anyway?). |
I'm not sure if you want to add a warning to the documentation, or use a fresh object in future to make the code more robust, but this totally caught me out.
To save me creating a custom referenceMany endpoint, I just did this in the dataProvider:
(compared to getList:
getList: (resource, params) => dataRequest(resource, "get_list", params)
, it just adds to the filter)But, the filter object is actually reused across requests, so each time I navigate to a different object, I was actually adding another filter value, thus breaking the requests.
Fixed by doing:
The text was updated successfully, but these errors were encountered: