GithubHelp home page GithubHelp logo

Comments (2)

ndrwksr avatar ndrwksr commented on July 1, 2024

I just did something similar yesterday, I am implementing a refresh feature and want to trigger a re-render when the refresh is done. Here is the solution I came up with:

// things.hooks.ts
const useRefreshing = () => useApp(R.prop('refreshing'))

export const useMyThings = (): ThinThing[] => {
  const fetchThings = useFetchThings()
  useRefreshing() // This causes this hook to execute and thereby rerender any time refreshing changes
  return suspend(fetchThings, fetchThingsKey)
}

export const useRefreshThings = (): AppState['refreshThings'] =>
  useApp(R.prop('refreshThings'))



// things.store.ts
// This is part of a Zustand store, another wonderful pmndrs project :)
export const createThingsSlice: StateSlice<ThingsSlice> = (set, get) => {
  ...

  const clearThings = () => clear(fetchThingsKey)

  const refreshThings = async () =>
    new Promise<void>(async (res) => {
      set({ refreshing: true })
      await get().refreshUser()
      clearThings()
      preload(
        () =>
          fetchThings().then((things) => {
            res()
            set({ refreshing: false })
            return things
          }),
        fetchThingsKey
      )
    })

  return {
    refreshing: false,
    fetchThings,
    refreshThings,
    clearThings,
  }
}

This approach doesn't quite accomplish what I want for refreshing (Suspense briefly renders the fallback), but it works great for re-rendering on clear. You could easily replace the store with useState, I have other reasons for using stores. The refreshThings function is a little ugly because I want to be able to await a call to refreshThings and have it resolve when the new data are available.

from suspend-react.

ndrwksr avatar ndrwksr commented on July 1, 2024

A quick update... You can avoid the Suspend fallbacks flashing by changing set({ refreshing: false }) to setTimeout(() => {set({ refreshing: false })}, 0)

The flashing of the Suspend fallbacks occurs because calling set({refreshing: false}) before the promise returned by fn is resolved never gives suspend-react a chance to cache what the promise resolves to before set({refreshing: false}) queues a re-render with stale data. Then-ing a promise puts a job in the job queue (which is executed immediately) whereas setTimeoutputs a task in the task queue, which is not evaluated until the job queue is empty. So because query caches the result of the promise returned by fn in a then call, if the promise returned by fn is resolved before suspend is called again, the fresh data will be immediately available, and suspend will never throw a promise and causing a suspension. Using setTimeout to make the call to set({refreshing: false}) a task instead of a job will result in query finishing its execution before set({refreshing: false}) is called, so when a re-render is triggered, the fresh data have already been cached. At least, that's my best guess as to what's going on!

from suspend-react.

Related Issues (14)

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.