React: TypeError: null 不是对象(正在评估“event.target.id”)

发布于 2025-01-13 07:43:38 字数 2794 浏览 0 评论 0原文

我正在尝试将表单输入保存到状态对象中。

const [newMovie, setNewMovie] = useState({
    name: "",
    ratings: "",
    duration: "",
  });

const handleChangeEvent = (event) => {
    console.log(event.target.value);
    setNewMovie((prevState) => {
      return { ...prevState, [event.target.id]: event.target.value };
    });
    console.log(newMovie);
  };

这是针对这种形式的调用:

    <form onSubmit={handleFormSubmit}>
      <div className="layout-column mb-15">
        <label htmlFor="name" className="mb-3">
          Movie Name
        </label>
        <input
          type="text"
          id="name"
          placeholder="Enter Movie Name"
          data-testid="nameInput"
          value={newMovie.name}
          onChange={handleChangeEvent}
        />
      </div>
      <div className="layout-column mb-15">
        <label htmlFor="ratings" className="mb-3">
          Ratings
        </label>
        <input
          type="number"
          id="ratings"
          placeholder="Enter Rating on a scale of 1 to 100"
          data-testid="ratingsInput"
          value={newMovie.ratings}
          onChange={handleChangeEvent}
        />
      </div>
      <div className="layout-column mb-30">
        <label htmlFor="duration" className="mb-3">
          Duration
        </label>
        <input
          type="text"
          id="duration"
          placeholder="Enter duration in hours or minutes"
          data-testid="durationInput"
          value={newMovie.duration}
          onChange={handleChangeEvent}
        />
      </div>
    </form>

问题是,当我开始输入第一个单词之后,React 会抛出错误:“TypeError: null is not an object (evaluating 'event.target.id')”和几个警告弹出:

在此处输入图像描述

具体错误如下。我不确定为什么会发生这种情况,因为 id 参数在表单中明确定义。谁能帮我解决这个问题吗?

输入图片此处描述

更新 1

记录整个 event.target 显示存在“id”参数(根据我的理解):

在此处输入图像描述

更新 2

尝试提取在调用设置状态函数之前的键和值,但现在我无法在输入字段中输入任何内容。请注意:它说“myKey”没有在任何地方使用,尽管事实上我实际上是在更新状态函数中使用它。

浏览器日志:

“在此处输入图像描述"

在此处输入图像描述

I'm trying to save form input into the state object.

const [newMovie, setNewMovie] = useState({
    name: "",
    ratings: "",
    duration: "",
  });

const handleChangeEvent = (event) => {
    console.log(event.target.value);
    setNewMovie((prevState) => {
      return { ...prevState, [event.target.id]: event.target.value };
    });
    console.log(newMovie);
  };

This is called for this form:

    <form onSubmit={handleFormSubmit}>
      <div className="layout-column mb-15">
        <label htmlFor="name" className="mb-3">
          Movie Name
        </label>
        <input
          type="text"
          id="name"
          placeholder="Enter Movie Name"
          data-testid="nameInput"
          value={newMovie.name}
          onChange={handleChangeEvent}
        />
      </div>
      <div className="layout-column mb-15">
        <label htmlFor="ratings" className="mb-3">
          Ratings
        </label>
        <input
          type="number"
          id="ratings"
          placeholder="Enter Rating on a scale of 1 to 100"
          data-testid="ratingsInput"
          value={newMovie.ratings}
          onChange={handleChangeEvent}
        />
      </div>
      <div className="layout-column mb-30">
        <label htmlFor="duration" className="mb-3">
          Duration
        </label>
        <input
          type="text"
          id="duration"
          placeholder="Enter duration in hours or minutes"
          data-testid="durationInput"
          value={newMovie.duration}
          onChange={handleChangeEvent}
        />
      </div>
    </form>

Issue is as soon as I start typing, right after the first word, React throws the error: "TypeError: null is not an object (evaluating 'event.target.id')" and a couple warnings pop up:

enter image description here

The exact error is as follows. I'm not sure why this is happening because the id parameter is clearly defined in the form. Can anyone help me fix this problem?

enter image description here

Update 1

Logging the entire event.target shows that "id" parameter is present (as per my understanding):

enter image description here

Update 2

Tried extracting the keys and value prior to calling the set state function but now I'm unable to enter anything in the input fields. Please note: It says that "myKey" is not used anywhere despite the fact that I'm literally using it inside the update state function.

Browser logs:

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文