You need to enable JavaScript to run this app. React使用react-hook-form提交表单时email、token取值为空的问题
优惠活动
大模型
产品
解决方案
定价
更多

React使用react-hook-form提交表单时email、token取值为空的问题

问题根源排查

以下是直接导致表单取值为空、提交异常的核心错误点:

  • 逻辑冲突:混用受控组件状态与react-hook-form托管逻辑
    你在input元素上同时做了两套状态绑定:一套是手写的value={email} + onChange修改本地state的原生React受控逻辑,另一套是通过register("email")把input注册给react-hook-form的托管逻辑。两套状态控制机制作用在同一个输入框上会互相覆盖、干扰,直接导致字段取值异常。
  • 参数认知错误:误解handleSubmit传入的提交函数参数
    当你用<form onSubmit={handleSubmit(onSubmit)}>绑定表单时,react-hook-form会先拦截原生提交事件、自动执行阻止默认行为的逻辑,再把收集到的所有表单字段值作为参数传给你写的onSubmit函数。你现在的代码里把这个参数当成了原生事件对象,还执行e.preventDefault(),本质上是对非事件对象做方法调用,很容易触发执行异常,中断提交流程。
  • 传参规则错误:Axios.post的参数顺序使用不当
    Axios的post方法标准入参是(请求地址, 请求体数据, 请求配置项),你现在的代码把email当请求体传、把token当请求配置项传,就算前面取值正常,后端也无法接收到完整的提交内容。
修正实现

按照react-hook-form的设计逻辑优化代码,去掉冗余的手写受控状态,正确对接提交流程:

import React from "react";
import { useForm } from "react-hook-form";
import Axios from "axios";

const CreateLink = () => {
  const {
    handleSubmit,
    register,
    formState: { errors }
  } = useForm();

  // onSubmit接收的是react-hook-form收集好的表单数据对象
  const onSubmit = async (formData) => {
    try {
      // 按Axios要求把所有字段拼成请求体,作为第二个参数传入
      const res = await Axios.post(
        "http://localhost:8000/service/createlink",
        {
          email: formData.email,
          token: formData.token
        }
      );
      if (res.data.success) {
        console.log("Link token created:" + res.data.success);
      }
    } catch (err) {
      console.log(err);
    }
  };

  return (
    <div className="App">
      <h1>Create Link</h1>
      <form onSubmit={handleSubmit(onSubmit)} className="linkForm inputForm">
        <div className="inputField">
          {/* 删掉手动绑定的value、onChange逻辑,完全交给register托管 */}
          <input
            name="email"
            placeholder="email"
            type="text"
            {...register("email", {
              required: "Email is required",
              pattern: {
                value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,
                message: "Invalid email address"
              }
            })}
          />
          <span className="loginErrorTextFormat">
            {errors.email && <p>{errors.email.message}</p>}
          </span>
        </div>
        <div className="inputField">
          <input
            placeholder="token"
            name="token"
            type="text"
            {...register("token", {
              required: "Token is required"
            })}
          />
          <span className="loginErrorTextFormat">
            {errors.token && <p>Input is not valid</p>}
          </span>
        </div>
        <input type="submit" />
      </form>
    </div>
  );
};

export default CreateLink;

补充说明:如果你确实需要在其他业务逻辑里读取input的取值,不要直接给input绑定固定value值,可以通过react-hook-form内置的watch方法监听字段变化,避免和register逻辑冲突。


内容的提问来源于stack exchange,提问作者soccerway

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 09:06:07