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

Application Crash with useRemember Hook During Browser History Navigation #1766

Open
Hasan-Mir opened this issue Jan 8, 2024 · 4 comments · May be fixed by #1784
Open

Application Crash with useRemember Hook During Browser History Navigation #1766

Hasan-Mir opened this issue Jan 8, 2024 · 4 comments · May be fixed by #1784
Labels
react Related to the react adapter

Comments

@Hasan-Mir
Copy link

Hasan-Mir commented Jan 8, 2024

Version:

  • @inertiajs/react version: 1.0.14

Describe the problem:

The application crashes when navigating through the browser history using the go back & go forward buttons if the useRemember hook is used on a page. If we are on a page that uses the useRemember hook, if we navigate to another page by entering the page’s URL in the address bar (i.e., a full page visit, not using Inertia’s Link component) and then click on the go back button next to the address bar, the application throws an error and displays a white screen:
image

This issue occurs with both Vite and Laravel-Mix. The only difference is that with Vite, the application doesn’t break and display a white screen.

I’ve created a repo to demonstrate this bug on the /login page. The repo has two branches: main for the Laravel-Mix version and vite-version for the Vite version.

export default function Login({ status, canResetPassword }) {
    const { data, setData, post, processing, errors, reset } = useForm({
        email: "",
        password: "",
        remember: false,
    });

    const [count, setCount] = useRemember(0, "count");

    // ...
}

There is no difference between providing a key (the second argument, in this case "count") to useRemember and not providing one at all.

Laravel-Mix version:

laravel-mix.mp4

When debugging the app.jsx file, it can be observed that during the first render after navigating back to /login from /register, the name argument of the resolve function is Auth/Login. However, during the second render (which I don't know why Inertia renders the components twice!!!), the name argument is undefined, which causes the error.

laravel-mix-debug.mp4

Vite version:

vite.mp4

The same issue occurs when debugging the app.jsx file in the Vite version.

vite-debug.mp4

By the way, this bug doesn’t show up when you’re using the Vue version of Inertia.

Steps to reproduce:

1- Create an Inertia project.
2- Use the useRemember hook in a component
3- Visit the page that includes the component using the useRemember
4- Enter an arbitrary URL in the address bar (either within your own app or another website) and hit enter, now click on the go back button next to the address bar.

Alternatively, you can clone this repo, which has two branches: main for the Laravel-Mix version and vite-version for the Vite version.

@Hasan-Mir Hasan-Mir added the react Related to the react adapter label Jan 8, 2024
@rick-nu
Copy link

rick-nu commented Apr 8, 2024

@reinink could you take a look at the proposed PRs? We're running into this issue for our project, and the proposed PRs seem to fix the issue.

Thanks in advance!

@driesvints
Copy link
Contributor

@rick-nu do you might know which of the PR's is a better solution?

@rick-nu
Copy link

rick-nu commented Apr 8, 2024

@driesvints #1784 seems the complete fix 👌

@rick-nu
Copy link

rick-nu commented Apr 15, 2024

We are now migrating away from useRemember because of this issue. We will be using useState instead. Less ideal but at least that doesn't break.

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
3 participants