加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 在React JSX中将对象作为组件属性

发布时间:2020-12-15 20:18:39 所属栏目:百科 来源:网络整理
导读:一个React新手的实验: React.renderComponent( MyComponent item={name: "A Name",description: "---"} /,document.getElementById('container')); 控制台错误: JSX value should be either an expression or a quoted JSX text 似乎没有这样的工作.我已经
一个React新手的实验:

React.renderComponent(
  <MyComponent item={name: "A Name",description: "---"} />,document.getElementById('container')
);

控制台错误:

JSX value should be either an expression or a quoted JSX text

似乎没有这样的工作.我已经挖了React文档一段时间但没有骰子.

解决方法

您没有正确传递item的值. foo = {…}表示JSX中的表达式,即prop值应该被评估为JavaScript.然后您缺少对象文字的{…}.它应该是

<MyComponent item={{name: "A Name",description: "---"}} />
//                 ^---       object literal       ---^
//                ^-----        expression        -----^

或者,如果您没有发现该语法非常易读,则可以先将对象分配给变量:

var item = {name: "A Name",description: "---"};
// ...
<MyComponent item={item} />

见https://facebook.github.io/react/docs/jsx-in-depth.html#attribute-expressions

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读