reactjs - 如何在 React 中创建确认删除弹出窗口?

标签 reactjs modal-dialog popup

我正在创建一个 Todo App ,并尝试创建一个 确认删除弹窗当用户想要删除待办事项时,这将是可见的。
在我的 todo.js组件我创建了一个 onClick回调,handleDelete , 在我的删除按钮中,该回调会将弹出窗口设置为 true 使其可见,问题是在我的 handleDelete 中我通过了Id作为参数,因此我可以跟踪单击了哪个待办事项并对其进行过滤以显示更新待办事项状态的新数据,但我只想在用户单击弹出窗口中的确认按钮时更新数据。
应用组件 :

function App() {
  const [inputValue, setInputValue] = useState("");
  const [todos, setToDos] = useState([]);
  const [noToDo, setNoToDo] = useState(false);
  const [popup, setPopup] = useState(false);

  const handleOnSubmit = (e) => {
    e.preventDefault();

    setNoToDo(false);
    const ide = nanoid();
    const date = new Date().toISOString().slice(0, 10);
    const newToDo = { task: inputValue, id: ide, date: date };
    setToDos([...todos, newToDo]);
    setInputValue("");
  };

  const handleDelete = (id) => {
    setPopup(true);

    let filteredData = todos.filter((todo) => todo.id !== id);

    {
      /*
    filteredData is the new data, but I only want to update 
    todos with filteredData when the user has clicked on the confirm
    button in the modal component, which execute(handleDeleteTrue)*/
    }
  };

  const handleDeleteTrue = () => {
    setPopup(false);
    setToDos(filteredData);
  };

  const handleEdit = (id, task) => {
    setInputValue(task);
    const EditedData = todos.filter((edited) => edited.id !== id);
    setToDos(EditedData);
  };

  return (
    <div className="App">
      <div className="app_one">
        <h1>To do app</h1>
        <form action="" className="form" onSubmit={handleOnSubmit}>
          <input
            type="text"
            placeholder="Go to the park..."
            onChange={(e) => setInputValue(e.target.value)}
            value={inputValue}
          />
          <button type="submit">ADD TO DO</button>
        </form>
      </div>
      {noToDo && <FirstLoad />}
      {todos.map((todo) => {
        return (
          <div key={todo.id} className="result">
            <Todo
              {...todo}
              handleDelete={handleDelete}
              handleEdit={handleEdit}
            />
          </div>
        );
      })}
      {popup && <Popup handleDeleteTrue={handleDeleteTrue} />}
    </div>
  );
}

export default App;
待办组件:
const Todo = ({ handleDelete, handleEdit, task, id, date }) => {
  return (
    <>
      <div className="result_text">
        <h3>{task}</h3>
        <p className="result_textP">{date}</p>
      </div>
      <div>
        <button onClick={() => handleEdit(id, task)} className="button green">
          Edit
        </button>
        <button onClick={() => handleDelete(id)} className="button">
          delete
        </button>
      </div>
    </>
  );
};

export default Todo;
模态组件:
function Popup({ handleDeleteTrue }) {
  return (
    <div className="modal">
      <div className="modal_box">
        <p>You sure you wanna delete?</p>
        <button className="modal_buttonCancel">Cancel</button>
        <button onClick={handleDeleteTrue} className="modal_buttoDelete">
          Confirm
        </button>
      </div>
    </div>
  );
}

export default Popup;
我试图声明 filteredData全局变量,在我的之外 App组件,所以当我执行 handleDelete它使用过滤后的数据初始化该变量,并且只有当用户单击弹出窗口上的确认按钮时,它才会执行一个新函数,handleDeleteTrue ,将数据更新为 filteredData .
它有效,但在我的组件之外声明变量不是一个好习惯,所以 有更好的方法吗?

最佳答案

问题 在您当前的代码中,您是 id应该被删除,所以你需要将它存储在 ref 中或 state .
这是一个 解决方案 存储 id状态以及显示/隐藏确认框的 bool 标志:

const [popup, setPopup] = useState({
  show: false, // initial values set to false and null
  id: null,
});
将删除处理程序修改为:
// This will show the Cofirmation Box

const handleDelete = (id) => {
  setPopup({
    show: true,
    id,
  });
};

// This will perform the deletion and hide the Confirmation Box

const handleDeleteTrue = () => {
  if (popup.show && popup.id) {
    let filteredData = todos.filter((todo) => todo.id !== popup.id);
    setToDos(filteredData);
    setPopup({
      show: false,
      id: null,
    });
  }
};

// This will just hide the Confirmation Box when user clicks "No"/"Cancel"

const handleDeleteFalse = () => {
  setPopup({
    show: false,
    id: null,
  });
};
并且,在 JSX 中,将处理程序传递给 Popup :
{popup.show && (
  <Popup
    handleDeleteTrue={handleDeleteTrue}
    handleDeleteFalse={handleDeleteFalse}
  />
)}

关于reactjs - 如何在 React 中创建确认删除弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67045902/

相关文章:

jquery - 将 HTML 数据属性传递给语义 UI 模式

javascript - LeanModal.js - X 单击时关闭模态框

css - 在 css html 中创建类似 msg pop 的边框

node.js - facebook库的keyMirror Node 包部分有什么用?

reactjs - redux 操作类型具有可选的有效负载属性-typescript 在 reducer 内提示

javascript - jQuery Modal 仅在单击后打开一次

javascript - 关闭和打开 chrome 扩展弹出窗口时如何不丢失数据

jquery - 如何在 jquery mobile 中将弹出窗口定位在屏幕顶部或固定在顶部?

reactjs - React Hooks - 修改后的状态不会立即反射(reflect)出来

javascript - 如何正确从 firebase 访问 uid 以将记录添加到另一个数据库中?