Skip to content
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

Deferred component throws error after a page (partial) reload #2221

Open
KaioFelps opened this issue Jan 31, 2025 · 5 comments · May be fixed by #2223
Open

Deferred component throws error after a page (partial) reload #2221

KaioFelps opened this issue Jan 31, 2025 · 5 comments · May be fixed by #2223
Labels
react Related to the react adapter

Comments

@KaioFelps
Copy link

KaioFelps commented Jan 31, 2025

Version:

  • @inertiajs/react version: 2.0.3

Describe the problem:

Currently, when using Deferred component in a page, if a navigation happens after the deferred props has loaded (e.g., a form submitting or another partial reload) the Deferred children component causes an error by trying to access the property.

My guess is: the new response overrides the page props, defining the deferred prop back to undefined, what leads to the error. Indeed, the Deferred component swap the children component with the fallback in these cases, but by this time, the children component has already tried to "read a property of undefined" and caused the error.

Indeed, using optional chaining to read the property from inside the children component (someProp?.map(...)) avoids the error and let Deferred bring back the fallback while it refetches the deferred prop.

Steps to reproduce:

If the page contains a Deferred component, a partial reload (triggered programmatically or by a form submission to a route that redirects back) can be enough to cause this error:

import { router, usePage, Deferred } from "@inertiajs/react"

export function Index() {
  function handleTriggerPartialReload() {
   router.get("/") // basically, a reload. Could even be a form submitting to a route that returns `redirect()->back()` 
 }

  return (
    <div>
      <Deferred data="users" fallback={<span>Loading...</span>}><DeferredUsers/></Deferred>
      <button onClick={handleTriggerPartialReload}>Trigger a partial reload</button>
    </div>
  )
}

export function DeferredUsers() {
  const props = usePage().props
  
  return (
    <div>
      {/* if it was props.users?.map instead, it wouldn't throw the error and the fallback would be shown again */}
      { props.users.map((user) => ( <span key={user.id}>{user.name}</span> )) }
    </div>
  )
}
@Verox001
Copy link

Verox001 commented Feb 1, 2025

Great, hope this pull request gets accepted soon!

@muhammedjafer
Copy link

@KaioFelps , i have the same problem with inertia/vue in live, but locally everything is working

@KaioFelps
Copy link
Author

@muhammedjafer does it also happen when you serve locally as if production (npm run build instead npm run dev)? I tested @inertiajs/vue3 Deferred component and it worked fine

@muhammedjafer
Copy link

@KaioFelps , i don't use deferred, not at all, locally when i do npm run build it works just fine but in the live it does not even though i run npm run build there too,

@KaioFelps
Copy link
Author

I don't think this is related with this issue then, the problem described here is caused by Deferred component. You should ask for help with more details about the problem you're facing in the Inertia official Discord server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react Related to the react adapter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants