简体   繁体   English

Onchange 值在反应中没有改变

[英]Onchange value is not changing in react

In the react application.在反应应用程序中。

This file is gonna update the to-do list in the application此文件将更新应用程序中的待办事项列表

when I click on Edit button this component is pop-up the Modal.当我单击编辑按钮时,此组件会弹出模态框。

The file name is EditTodos.js文件名为EditTodos.js

import React, { useState } from "react";
import { Button, Modal } from "react-bootstrap";

export default function EditTodo({ todo }) {
  const [description, setDescription] = useState(todo.description);
  const [show, setShow] = useState(false);

  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);

  const updateDescription = async () => {
    try {
      const body = { description };
      const update = fetch(`http://localhost:3000/${todo.todo_id}`, {
        method: "PUT",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(body),
      });
    } catch (error) {
      console.log(error.message);
    }
  };
  return (
    <>
      <Button
        variant="warning"
        onClick={handleShow}
        data-target={`#id${todo.todo_id}`}
      >
        Edit
      </Button>

      <Modal show={show} onHide={handleClose} id={`id${todo.todo_id}`}>
        <Modal.Header closeButton>
          <Modal.Title>Edit Todo</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <input
            type="text"
            className="form-control"
            value={todo.description}
            onChange={(e) => setDescription(e.target.value)}
          />
        </Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={handleClose}>
            Close
          </Button>
          <Button variant="primary" onClick={handleClose}>
            Save Changes
          </Button>
        </Modal.Footer>
      </Modal>
    </>
  );
}

So I am not able to change the input field value.所以我无法更改输入字段值。

onChange={(e) => setDescription(e.target.value)}

I have done this but still I am not able to change input field value?我已经这样做了,但我仍然无法更改输入字段值?

whats going wrong in here.这里出了什么问题。

you shouldn't define state using props, thus you are having two different sources of truth , the onChange function should come from parent, though this is a bad practice the solution for this particular case would be您不应该使用道具定义 state ,因此您有两个不同的事实来源, onChange function 应该来自父母,尽管这是一种不好的做法,这种特殊情况的解决方案将是

          <input
            type="text"
            className="form-control"
            value={description}
            onChange={(e) => setDescription(e.target.value)}
          />

the reason being you are changing the description state in onChange but you are always keeping the value as description.toDo, which you are getting from parent component.原因是您正在更改 onChange 中的描述 state 但您始终将值保留为 description.toDo,这是您从父组件获取的。

however, avoid doing this , keep a single source of truth, pass the onChange function as prop.但是,请避免这样做,保持单一事实来源,将 onChange function 作为道具传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM