You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React中如何将状态值传递给子组件CreateUser?props.data为undefined

解决React子组件props.data为undefined的问题

你的问题出在状态变量的访问方式错误:

在One组件中,info1是通过useState声明的组件内部状态,但你传递给CreateUser时写成了props.info1——props是父组件传给One的参数,里面根本没有info1这个属性,所以子组件拿到的props.data自然是undefined。

修复方法

直接传递组件内部的info1变量即可:

const One = (props) => {
  const [info1, setInfo1] = useState({
    firstname: "",
    lastname: "",
    email: "",
    mobnumber: "",
    password: "",
  });
//....

  return (
    <div>
      <div className="container">
        <div className="row ">
          <div className="col-4">
            <CreateUser
              onInputChanged={onInputChanged}
              data={info1} {/* 这里改为直接使用info1 */}
            ></CreateUser>
          </div>
          <div className="col">
            <SelectParker />
            <br />
            <ActionButtons {...props} nextStep={validate} />
          </div>
        </div>
      </div>
    </div>
  );
}

补充说明

  • 组件内部通过useState创建的状态变量,直接用变量名访问即可,不需要通过props
  • 只有父组件传递过来的数据,才需要通过props.xxx获取

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 15:55:53