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

Form Being Reset #1704

Closed
ramonmalcolm10 opened this issue Oct 17, 2023 · 4 comments
Closed

Form Being Reset #1704

ramonmalcolm10 opened this issue Oct 17, 2023 · 4 comments
Labels
react Related to the react adapter

Comments

@ramonmalcolm10
Copy link

Version:

  • @inertiajs/react version: 1.0.12

Describe the problem:

useForm keeping resetting when I update a specific field from a component that utilize react-google-autocomplete library.

Steps to reproduce:

  1. Install react-google-autocomplete library
  2. Create custom input
import { usePlacesWidget } from "react-google-autocomplete";
import * as React from "react";

type Props = {
    onChanged: (address: string) => void;
    value: string;
};

const LocationInput: React.FC<Props> = ({
    onChanged,
    value,
}) => {
    const { ref } = usePlacesWidget<HTMLInputElement>({
        apiKey: import.meta.env.VITE_GOOGLE_MAP_KEY,
        onPlaceSelected: (place) => onChanged(place.formatted_address ?? ""),
        options: {
            fields: ["geometry", "formatted_address", "url", "vicinity"],
            types: ["address"],
        },
    });
    return (
        <input
            ref={ref}
            defaultValue={value}
        />
    );
};

export default LocationInput;
  1. Generate a Map API Key in Google Cloud Console and set the apiKey value
  2. Create parent component and add a new form that utilize the component
const form = useForm({
        name: "",
        address: "",
    });
return (
     <input value={form.data.name} onChanged={(e) => form.setData("name", e.target.value)}  />
     <LocationInput
          value={form.data.address}
          onChanged={(e) =>
              form.setData("address", e)
          }
     />
)
  1. Update the name field and then update the address field to simulate the issue
@ramonmalcolm10 ramonmalcolm10 added the react Related to the react adapter label Oct 17, 2023
@rikoriswandha
Copy link

I experienced this issue recently today.
Fixed by setting the data according to this #1086. So you can try this:

const form = useForm({
        name: "",
        address: "",
    });
return (
     <input value={form.data.name} onChanged={(e) => form.setData("name", e.target.value)}  />
     <LocationInput
          value={form.data.address}
          onChanged={(e) =>
              form.setData((data) => ({
                  ...data,
                  address: e,
              }))
          }
     />
)

@ramonmalcolm10
Copy link
Author

Any idea @rikoriswandha why it is making a consecutive call on a string field?

@rikoriswandha
Copy link

I have no idea, in my case, I'm using filepond-react and it keeps resetting the other fields' state. Probably related to the internal state of the library

@derrickreimer
Copy link
Contributor

We are now using an updater function internally with setData, so consecutive calls to set data should work as expected when the next release is cut (#1859)

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
Development

No branches or pull requests

3 participants