我有表格的代码
props = { user: {userattr1: 1, userattr2: 2}};
var element = React.createElement(MyReactClass, props);
即,其中 props 是一个嵌套对象。当我尝试编译上面的代码时出现错误:
警告:任何对键控对象的使用都应该在作为子对象传递之前包装在 React.addons.createFragment(object) 中。
我一直在网上查看,似乎嵌套对象作为 props 是完全允许的。我该如何解决我的错误?
编辑:MyReactClass
看起来像这样:
var MyReactClass = React.createClass({
render: function () {
<div>{this.props.user}</div>
}
})
最佳答案
我不认为你遇到的问题与作为 Prop 的嵌套对象有关。这是一个例子:
var Hello = React.createClass({
render: function() {
return <div>Hello {this.props.user.name}</div>;
}
});
var props = { user: {name: "World"}};
React.render(React.createElement(Hello, props), document.getElementById('container'));
更有可能的是,您的问题与您如何设置子组件的键有关。但是,如果不查看整个代码,就很难分辨。
这是 creeateFragment
函数的链接,它可能对您有所帮助。 https://facebook.github.io/react/docs/create-fragment.html
关于javascript - React.js : Passing nested props into React. 创建元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30440297/