question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inertia link does not reload the page form data

See original GitHub issue

If I am on an the edit page for Organizer A and click on an inertia link to edit Organizer B. Inertia replaces the url but the form is not reloaded and keeps Organizer A data (and vue blows up) (If I click on the link from any other page it works as expected.)

Example href: https://test-site.test/app/organizers/org-YSMqnF-L8VSq8s5jzZ-nB/edit

<inertia-link :href="app.organizers.edit " method="get">{{organizer.name}}</inertia-link>

OrganizerController@edit

public function edit(Organizer $organizer)
{
    return inertia('Organizers/Edit', [
        'organizer' => $organizer,
    ]);
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
rodrigopedracommented, Mar 31, 2020

Hi @tanthammar if you can please share some of your page component, at least props and the top level form or the container element that wraps the form.

In the meanwhile one thing you can try is adding a ':key prop to your form element to force Vue re-render the form if some props changes:

<form :key="organizer.id" ...>
... 
</form>

Other thing to check: you might be copying the organizer fields to an object in the component’s data() so you can use v-model in your form.

As the page component is kept the same the component won’t run the data() when just the prop changed. So you can use the updated() life-cycle hook to update the form fields, if that is the case.

For example:

<script>
export default {
  props: ['organizer'],

  data() {
    return {
      name: this.organizer.name,
      address: this.organizer.address,
    };
  },

  updated() {
    this.name = this.organizer.name;
    this.address = this.organizer.address;
  },
}
</script>

If you have more props that can change from the server prefer using a watcher over using the updated() lifecycle hook, otherwise you user can loose edits:

<script>
export default {
  props: ['organizer'],

  data() {
    return {
      name: this.organizer.name,
      address: this.organizer.address,
    };
  },

  watch {
    organizer(current, old) {
      if (current.id === old.id) return; // same organizer -> ignore

      // different one -> update form fields
      this.name = current.name;
      this.address = current.address;
    },
  },
}
</script>

Hope any of that helps.

EDIT updated watcher code

1reaction
reininkcommented, Oct 20, 2020

@IvanBernatovic So, this is actually being caused by Jetstream, not Inertia.js. It’s because Jetstream defaults form submission to { resetOnSuccess: true }, which is actually problematic if you submit a form back to the same page. Basically what happens is Jetstream captures the props on the initial component load and saves them in local memory. Then, when you make a request to submit the form, it returns back to the same component, which still has those original values in memory, and Jetstream restores those values.

Update your UserForm.vue file to fix this:

form: this.$inertia.form({
  name: this.user?.name || "",
  email: this.user?.email || "",
}, {
  resetOnSuccess: false,
}),

IMHO, Jetstream shouldn’t default resetOnSuccess to true, but rather this should be an opt-in thing you use when it’s needed (ie. resetting password inputs back to blank).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Partial reloads
Partial reloads. When making visits to the same page, it's not always necessary to fetch all of the data required for that page...
Read more >
Inertia: Reload page with updated data without modifying ...
The behaviour I would like is: a user scrolls down a page, clicks the completed button, and the back-end sends that newly updated...
Read more >
Inertia React, can't get updated data on redirect back ...
When the page loads initially, the basket is passed from Laravel to Vue when Inertia passes the props, and I have a created()...
Read more >
Inertia reloads whole page instead of SPA
Hello I am trying Inertia with Laravel Jetstream and Vue for a small project. All routes are behaving like a SPA expect one...
Read more >
The Ultimate Guide to Inertia.js
Inertia is similar to Vue Router, in that it allows you to move between pages without having to reload the entire page. However,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found