Giter Club home page Giter Club logo

Comments (13)

ntucker avatar ntucker commented on May 12, 2024

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.

binchik avatar binchik commented on May 12, 2024

@ntucker Yes, that's what I want. And I want to update the cache optimistically.

from data-client.

ntucker avatar ntucker commented on May 12, 2024

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.

 avatar commented on May 12, 2024

@ntucker, what if I want to call GET after each PATCH or simply call GET again on click?

from data-client.

ntucker avatar ntucker commented on May 12, 2024

@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.

 avatar commented on May 12, 2024

@ntucker, I thought my question is regarded to this issue. Should I create new issue?

from data-client.

ntucker avatar ntucker commented on May 12, 2024

@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.

 avatar commented on May 12, 2024

@ntucker yep, I referred to this scenario

from data-client.

ntucker avatar ntucker commented on May 12, 2024

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.

 avatar commented on May 12, 2024

@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.

ntucker avatar ntucker commented on May 12, 2024

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.

ntucker avatar ntucker commented on May 12, 2024

Docs updated https://resthooks.io/docs/api/useFetcher#updatefunction-sourceresults-destresults-destresults

Please tell me if this is clear!

from data-client.

ntucker avatar ntucker commented on May 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.