Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Zalo's avatar
Level 19

Reloading page and keep component alive

Hi people!

I'm here again with another InertiaJS doubt. I would like to use Inertia router/form helper to reload same page but with different data (filtering users).

InertiaJS docs got this partial reloads section where explains exactly what I want to implement.

I have done it, however, I don't like the way it works.

When I use partial reload functionality, my component gets destroyed :( I see component life cycle runs again (created and mounted) every time reloads occurs...

Are there a way to keep component alive?

This is what I have

A simple page component that receives random server data from props and render it in a list.

There is a button to fire reload request. Each time it reloads, the componet is destroyed and recreated :/

<script setup>
import { router, usePage } from '@inertiajs/vue3'
import { onMounted, toRefs } from 'vue'
import AppButton from '@/components/core/AppButton.vue'

const { props } = toRefs(usePage())

console.log('onCreated')

const refresh = () => {
  router.visit('test', {
    only: ['data'],
  })
}

onMounted(() => console.log('onMounted'))

</script>
<template>
  <div class="border rounded-lg p-6">
    <h1>This is Test Page</h1>
    <ul>
      <li v-for="item in props.data"
          :key="item"
          v-text="item"/>
    </ul>
    <app-button
      label="Refresh"
      class="mt-5"
      @click="refresh"
    />
  </div>
</template>

A Screenshot

Screenshot

Any help will be appreciated.

Thanks!

0 likes
4 replies
vincent15000's avatar

Can you show the controller from which you send the datas to the frontend please ?

Zalo's avatar
Level 19

Yeah, Its a very simple route just for testing this.

Route::get('test', function () {
   return \Inertia\Inertia::render('TestPage', [
       'data' => [
           rand(0, 10),
           rand(0, 10),
           rand(0, 10),
       ]
   ]);
});

I have checked following:

  • if route.reload({ data: 'always-same-value' }): It will not destroy component and will update prop! It's cool
  • if route.reload({ data: 'different-value-each-time'}): It will destroy component... I need to avoid component destruction here, because I want to send filters to backend

I also have seen that Layouts are never destroyed, so maybe I should refactor component to use Layout and <teleport>

1 like
Zalo's avatar
Level 19

@vincent15000 Thanks for your reply!

Finally I found the problem, and it was in my Layout. I had something like this to apply transitions

<v-container fluid>
  <Transition
    name="page"
    mode="out-in"
    enter-from-class="opacity-0"
    leave-to-class="opacity-0"
    class="transition ease-in-out duration-500"
    appear>
    <main :key="$page.url" class="container px-6 lg:px-8 mb-auto">
      <slot /> <!-- all Page Component get rendered here -->
    </main>
  </Transition>
</v-container>

The :key="$page.url" was forcing component to be recreated each time....

So I changed it to :key="$page.component" and now it works!!

I hope it helps someone else.

1 like

Please or to participate in this conversation.