Comments (13)
Thanks for the report!
Let me try to see if I understand this correctly: when you send a patch request to /sort it will changes the sort order of future calls to GET /.
So what you want is to be able to know that this will change from the patch and update the rest hooks cache for all GET / requests to return the new sort order.
Can you confirm that this sounds like I understand your situation correctly? Thanks!
from data-client.
@ntucker Yes, that's what I want. And I want to update the cache optimistically.
from data-client.
Ok, I'll include the capability and docs for special cases like this when I implement the more common-case optimistic updates in #96
from data-client.
@ntucker, what if I want to call GET after each PATCH or simply call GET again on click?
from data-client.
@St1myL I feel like I'm missing the first half of this conversation. With regard to what are you asking about this scenario?
from data-client.
@ntucker, I thought my question is regarded to this issue. Should I create new issue?
from data-client.
@St1myL I guess I'm just lost because if you're referring to this scenario, then the answer is in the setup:
"Let me try to see if I understand this correctly: when you send a patch request to /sort it will changes the sort order of future calls to GET /."
from data-client.
@ntucker yep, I referred to this scenario
from data-client.
If you do a GET after, it will have the correct results from the server so everything will work well. The purpose of optimistic updating is so you can have those correct results without having to do another request. Without it you can still make your application work by initiating subsequent fetches - however this is undesirable as it adds complexity and latency to the user experience. This is a reasonable workaround until this lands however.
Currently mutations and deletes are optimistically applied. However, creates appending to lists, and this case of changing a sort order, are not. The new functionality should make the create case very simple, and give low-level extensions to make any arbitrary optimistic application possible.
Does that address your question @St1myL ?
from data-client.
@ntucker yes, I finally get it. Thank you for the explanation, still new at this. My task was to update data on the page but I have a different approach to deal with it.
from data-client.
Not sure about the specifics of your endpoint, but in 2.2 You can now add custom update functions like so:
function mergeArticles(
sortEndpointResponse: any,
resultsFromCache: string[],
): string[] {
const ret = [...resultsFromCache];
ret.sort();
return ret;
}
function useSortFetcher(params) {
const sort= useFetcher(MyResource.sortShape());
return useCallback(() => {
return sort({}, params, [
[MyResource.listShape(), {}, mergeArticles],
]);
}, [sort]);
}
A few notes: resultsFromCache is going to be the ResultType<typeof MyResource.listShape().schema>
. In other words, the normalized form of the actual response for list request with entities turned into their ids. Return value must match. I used the simple string[] as example but could very well be different for you.
I implemented a hook here that returns a function to call that will both do the fetch and automatically provide the proper sort as well. This is seen by using the new third parameter to the fetch()
function returned from useFetcher() hook.
PS) We needed a lot of features internally so haven't fully documented these parts on the public docs site yet, but that will come later.
@St1myL @binchik Please tell me if this works for you! Using these functions are powerful and are strictly typed so you shouldn't have to worry if you're using typescript but if not you should be very careful as a messed up state will ruin the whole library.
from data-client.
Docs updated https://resthooks.io/docs/api/useFetcher#updatefunction-sourceresults-destresults-destresults
Please tell me if this is clear!
from data-client.
Gonna close this issue since it has landed. Feel free to open a new issue with any additional issues you may have!
from data-client.
Related Issues (20)
- ReferenceError: Can't find variable: endpoint after upgrading from 3.0.3 to 5.1.1 HOT 21
- bug: missing typing for body when using entity class HOT 3
- v6 migration guide HOT 4
- RFC: Retry on 'soft' errors + sideEffect false HOT 3
- Objects and properties sometimes undefined from useSuspense HOT 18
- Object.hasOwn is not a function on safari v14.1 HOT 2
- Missing type inference for controller.fetch HOT 20
- Pagination with infinite scroll example HOT 4
- Docs: Thinking in Suspense HOT 1
- Using function to define entities HOT 2
- useLoading is incompatible with React.StrictMode HOT 1
- Adding a child list item and updating the parent list HOT 4
- React Hook cannot be called in a class component. HOT 2
- Question: Is useCache supposed to automatically return the updated cache data after cache change? HOT 2
- Cannot import makeCacheProvider in rest-hook version 7 for hook unit tests HOT 6
- Action Required: Fix Renovate Configuration
- Unexpected behavior when an entity has many entities. HOT 1
- Support Next.js app router HOT 4
- add create member to resource class HOT 8
- Allow to override Endpoint url method to handle objects in query params HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from data-client.