Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Controller FormData TextInput doen't reset

The problem: I'm on the overview and I select a sensor. I edit the name of a sensor. I get send back to overview of all sensors, the name is indeed changed. Then I click on another sensor and without changing anything I save it, then it gets the name of the previous changed sensor. So the field (which is required) is empty. But it still gets through and takes the name of the previous sensor. I have made a video of this problem -> https://www.youtube.com/watch?v=yH-4bW0JEWI.

I tried setting a defaultvalue, but the name of the previous sensor overwrites this if I don't change anything and do click on save. I also tried to reset FormData.name. It does get reset, but when I click on another sensor it magically gets filled with the name of the previous sensor.

So here is my code:

const onSubmit = (formData) => {
        data.name = formData.name;
        console.log("before formdata: " + formData.name); //when clicked on a new sensor, it gets the formdata from a previous sensor that has been edited
        formData.name = "";
        console.log("after formdata: " + formData.name); //formdata.name gets resetted
        putRequest();
    }

function putRequest() {
        setIsLoading(true)
        axios.put(`api/sensor/${data.sensorId}`, data)
        .then(() => {
            navigation.push("Overview");
        })

and in the return:

<Controller 
    control={control}
    defaultValue={data.name}
    render={({onChange}) => (
        <TextInput 
            style={component.textInput}
            //defaultValue={data.name}
            placeholder={data.name}
            autoCapitalize="sentences"
            returnKeyType="next"
            onChangeText={(e) => onChange(e)}
            clearButtonMode="always"
        />
    )}
    name="name"
    rules={{}}//deleted rules for overview
/>

So does anyone know why FormData.Name gets set to the previous sensor when I dont provide a value?

like image 983
klaas0 Avatar asked Jun 13 '26 23:06

klaas0


1 Answers

Still have the problem when you assign rules? not null etc.

like image 80
Takkie253 Avatar answered Jun 15 '26 12:06

Takkie253