You need to enable JavaScript to run this app. React中传对象属性为props时组件无法渲染,报对象不能作为React子元素错误如何解决
优惠活动
大模型
产品
解决方案
定价
更多

React中传对象属性为props时组件无法渲染,报对象不能作为React子元素错误如何解决

问题原因

React 函数组件的第一个入参固定是全量 props 对象,你在定义Test组件时写的const Test = (title) => {},这里拿到的title不是你传入的字符串值,是包含所有传入属性的对象{title: "test title"},直接把对象放到 JSX 里渲染就触发了报错。

修复方案

有两种常用的正确写法:

  • 入参时直接解构取出对应属性
const Test = ({ title }) => {
  return (
    <article>
      <h4>{title}</h4>
    </article>
  );
};
  • 先接收全量props对象,再取值调用
const Test = (props) => {
  return (
    <article>
      <h4>{props.title}</h4>
    </article>
  );
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 02:09:04