inertiajs / inertia

Inertia.js lets you quickly build modern single-page React, Vue and Svelte apps using classic server-side routing and controllers.
https://inertiajs.com
MIT License
6.3k stars 423 forks source link

IsDirty keep at true after form submit #1828

Closed Oui-Dev closed 6 months ago

Oui-Dev commented 6 months ago

Versions:

Describe the problem:

For a Searchbar component I make a form with many fields but when I submit it (using form.get) isDirty keep at true if preserveState is set to true. I don't know if I make something wrong with my default values or with the debounce.

I saw someone with a similar issue here https://github.com/inertiajs/inertia/discussions/1033 but I don't understand how he solve it.

Code

    const allParams = usePage().props.route.query;
    const currentRouteName = usePage().props.route.name;

    const form = useForm({
        search: allParams.search || '',
        searchBy: allParams.searchBy ?? searchByOpts[0].key,
        orderBy: allParams.orderBy || 'DESC'
    });

    const [debounceTimeout, setDebounceTimeout] = useState(null);

    useEffect(() => {
        console.log('isDirty', form.isDirty)
        if(form.isDirty) {
            console.log(form.data);

            // Clear the previous debounced function
            if (debounceTimeout) clearTimeout(debounceTimeout);

            // Create a new debounced function
            setDebounceTimeout(setTimeout(() => {
                form.get(route(currentRouteName), { preserveScroll: true, preserveState: true });
            }, 200));
        }
    }, [form.isDirty]);