skip to Main Content

I am trying to create an edit function for updating a task that was previously written.

I have tried this so far but apparently the prompt is only for the browser. Would this even work? What are alternatives to create the prompt for react native?



  const taskUpdate = (index) => {
     const newItemsCopy = [...taskItems];
     const item = newItemsCopy[index];
     let newItem = prompt(`Update ${item.task}?`, item.task);
     let todoObj = { todo: newItem, complete: false };
     newItemsCopy.splice(index, 1, todoObj);
     if (newItem === null || newItem === "") {
      return;
      } else {
      item.task = newItem;
      }
      setTaskItems(newTodoItems);
  }

  

Full Code

2

Answers


  1. You can use Alert from react-native.

    import { Alert } from "react-native";
    
    Alert.alert(
          "Alert Title",
          "My Alert Msg",
          [
            {
              text: "Cancel",
              onPress: () => console.log("Cancel Pressed"),
              style: "cancel"
            },
            { text: "OK", onPress: () => console.log("OK Pressed") }
          ]
        );
    Login or Signup to reply.
  2. You can implement this using Modal

    1. On long press, open the Modal which consists of textInput with value.
    2. Edit the value.
    3. Save the value on close/ handle it with save button inside Modal.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search