How to pass data from one component to TextInput of another component and still be able to edit the value in react native

I passed value from a component to the TextInput of the component below and set the data to the value prop of the TextInput but I'm not able to edit the value. If I delete a text or type something in the TextInput, it get replaced immediately with the value that I passed from the first component.

Here is the code

export default function EditTodoScreen({ route }) {
  const { title, description } = route.params
  const [state, setState] = useState({titleToUpdate: title, title: "", description: "" });

  const {isLoading, mutateAsync} = useUpdateTodo()

  //New way of making network call
  const handleSubmitUpdatedTodo = async () => {
    try {
      const response = await mutateAsync(state)
      showToast(response.data.message);
    } catch (error) {
      showToast(error);
    }
  };

  return (
    <View style={styles.container}>
      <View>
        <Text style={styles.inputHeader}>Title</Text>
        <TextInput
          placeholder="Please enter title"
          style={styles.input}
          allowFontScaling
          onChangeText={(title) => {
            setState({ ...state, title });
            {console.log(title)}
          }}
          value={title}
        />
        <Text style={styles.inputHeader}>Description</Text>
        <TextInput
          placeholder="Please enter description"
          style={styles.input}
          onChangeText={(description) => {
            setState({ ...state, description });
          }}
          value={description}
        />
      </View>
      <Button
        buttonStyle={styles.button}
        title="submit"
        onPress={() => {
          handleSubmitUpdatedTodo()
        }}
      />
      {isLoading ? <ActivityIndicator color="red" /> : null}
    </View>
  );
}

Please, how can I fix it?



Read more here: https://stackoverflow.com/questions/68462720/how-to-pass-data-from-one-component-to-textinput-of-another-component-and-still

Content Attribution

This content was originally published by user11874398 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: