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

vue3 usePage state not updating on props changing #1432

Closed
nurdism opened this issue Feb 5, 2023 · 5 comments · Fixed by #1469
Closed

vue3 usePage state not updating on props changing #1432

nurdism opened this issue Feb 5, 2023 · 5 comments · Fixed by #1469

Comments

@nurdism
Copy link

nurdism commented Feb 5, 2023

Version:

  • @inertiajs/vue3 version: 1.0.0

Describe the problem:

when using usePage() it does not return a ref and there for does not update correctly when the page changes

because usePage is returning page.value and not the ref once you reassign page.value the old value will not get updated.

Example:

use Inertia\Inertia;

Route::get('/test', function () {
  return Inertia::render('Test', [
    'time' => time(),
  ]);
});
<script setup>
  const page = usePage()
  const time = computed(() => page.props.time)
  const newTime = ref(0)

  let timeout
  function reload() {
    timeout = setTimeout(() => {
      console.log(page.props) // { time: "1675567188" }
      console.log(time.value) // "1675567188"

      const page2 = usePage() // call it again now that the props have changed
      console.log(page2.props) // shows true value

      newTime.value = page2.props.time // update newTime ref to show new time

      router.reload()
      clearTimeout(timeout)
      reload()
    }, 5000)
  }

  onMounted(() => {
    reload()
  })
</script>

<template>{{ time }} : {{ newTime }}</template>

this would be solved by returning the ref not the value, but would be a breaking change due to you needing to access page values via page.value I forgot about reactive objects

@nurdism nurdism changed the title usePage state not updating on props changing vue3 usePage state not updating on props changing Feb 5, 2023
@nurdism
Copy link
Author

nurdism commented Feb 5, 2023

Someone on discord pointed out, while true usePage() does return the current page meta, it is unusual pattern for vue 3 composable functions. Composable functions typically return some form of reactive object (or function) to be used. There's a lot of good info on reactive objects here.

Having to use something like this:

const time = computed(() => usePage().props.time)

vs

const page = usePage() // reactive object
const time = computed(() =>page.props.time)

the page ref could be made into a reactive object rather then a ref and updates would be reactive without having to use page.value

@nurdism
Copy link
Author

nurdism commented Feb 5, 2023

I have written and example on how this might be used, this may just work but I don't have an env set up to test using these changes https://gist.github.com/nurdism/cd162c5413de7f38d939ff4cba308b4e this assumes that page object always has component, props, url, version, scrollRegions, rememberedState & resolvedErrors keys, it reassigns each with Object.assign(page, args.page) you could also assign it like Object.assign(page, { component: '', props: null, (etc), ...args.page }) and this will add all the necessary default values and update the full object

@mathieutu
Copy link
Contributor

mathieutu commented Feb 26, 2023

Linked to discussion in this PR.

The usePage implementation is indeed problematic. It needs to be directly used in a computed for each usage.

@mathieutu
Copy link
Contributor

I've submitted a fix.

@reinink
Copy link
Member

reinink commented Apr 20, 2023

Hey folks, these issues have been fixed (see #1469), and have been released in v1.0.4. Thanks for all your input/help! 🙏

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

Successfully merging a pull request may close this issue.

3 participants