2015-05-25 35 views
0

我具有形式React.js:傳遞嵌套道具成React.createElement

props = { user: {userattr1: 1, userattr2: 2}}; 
var element = React.createElement(MyReactClass, props); 

即其中道具是嵌套對象的代碼。當我嘗試編譯上面的代碼時,出現以下錯誤:

警告:在作爲子對象傳遞之前,任何使用鍵控對象都應該包裝在React.addons.createFragment(object)中。

我一直在網上看,似乎嵌套的對象完全是作爲道具允許的。我該如何解決我的錯誤?

編輯:MyReactClass看起來是這樣的:

var MyReactClass = React.createClass({ 
    render: function() { 
    <div>{this.props.user}</div> 
    } 
}) 
+0

您不會收到錯誤,但會發出警告 –

+1

您可以在jsfiddle中重現此操作嗎?我認爲你的問題在別處,而不是道具。 MyReactClass如何定義? – wgcrouch

回答

3

我不認爲這個問題,你有是關係到一個嵌套的對象當道具。這是一個例子:

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')); 

https://jsfiddle.net/urjmndzk

更可能的是,你的問題是關係到你如何設置子組件的鑰匙。但是,如果沒有看到整個代碼就很難說清楚。

這是一個鏈接到creeateFragment函數,它可能會幫助你。 https://facebook.github.io/react/docs/create-fragment.html

+1

事實證明,問題在於,我試圖在HTML標記中使用對象,並假定React將使用其JSON表示。 – Allen

相關問題