2015-10-20 89 views
0

我試圖遍歷一個對象,並創建錶行動態地下面陣營的觀點是不會呈現在陣營0.14

var msg = React.createClass({ 

render: function() { 
    var profiles = []; 
    $.map(profs, function(prof){ 
     profiles.push(<tr> 
      <td>{prof.Name}</td> 
      <td>{prof.Type}</td> 
      </tr>) 

陣營0.14像,並嘗試在添加此

<table> 
    <tbody> 
     {profiles} 
    </tbody> 
</table> 

但它是沒有渲染也沒有拋出任何錯誤也。 我喜歡渲染

ReactDOM.render(React.createElement(msg, {data: this.model.attributes}), this.el); 

如果我刪除「{profiles}」它是正確渲染頁面的其他部分。 但是,如果使用相同的代碼在陣營0.13.2

React.render(React.createElement(msg, {data: this.model.attributes}), this.el); 

這是工作的罰款。 如果有人爲這類錯誤提出調試工具,它會更有幫助。

+1

除非我失去了一些東西,這看起來好了,你的錯誤可能是由於一些我們看不到這裏。偏離主題似乎你不明白每個地圖和地圖之間的區別,因爲你每個人都使用它,你也可以使用es5的地圖 –

+0

只是一個觀察:你爲什麼不分配'$ .map'的輸出?函數到'profiles'變量? 'var profiles = $ .map(profs,function(prof){return( ...);});'但是這可能無法解決您的問題。 – zwippie

+0

我也試過這種方式..但它是一樣的 –

回答

0

剛剛完成升級我的應用程序到0.14,當組件本身或其中一個子組件有一個未定義的混合時,我遇到呈現默默失敗。在我的情況下,這是反應路由器的棄用mixins ...但花了很長時間才弄明白。 React通常非常優雅地處理未定義或空值,或者至少在警告的時候處理,但顯然在ti到mixin時不會顯示。

0

我認爲這個問題是你的第一個代碼示例中map方法永遠不會關閉:最後)關閉push,這讓map和其回調(即function(prof))未閉合。

Here's a working component based on your code呈現正確:

var ExampleComponent = React.createClass({ 
    render: function() { 
    var profiles = $.map(this.props.profs, function (prof) { 
     return (
     <tr key={prof.name}> 
      <td>{prof.name}</td> 
      <td>{prof.type}</td> 
     </tr> 
    ); 
    }); 

    return (
     <table> 
     <thead> 
      <tr> 
      <th>Name</th> 
      <th>Type</th> 
      </tr> 
     </thead> 
     <tbody> 
      {profiles} 
     </tbody> 
     </table> 
    ); 
    } 
}); 

var profileObjects = [{name: 'Brendan Eich', type: 'Netscape'}, 
         {name: 'Jordan Walke', type: 'Facebook'} 
        ]; 

ReactDOM.render(
    <ExampleComponent profs={profileObjects} />, 
    document.getElementById('container') 
); 
+0

謝謝你的回覆。在jsfiddle中包含了「react-dom.js」和「react-with-addons.js」,但沒有包含react.js。但它是如何工作正常?對於這個簡單的操作,爲什麼我必須包含「addons.js」? –

+0

誠實地說,我只是把「[React Base Fiddle(JSX)](https://jsfiddle.net/reactjs/69z2wepo/)」分成了兩個這樣的庫。 React包含在'react-with-addons.js'中,這就是爲什麼小提琴能夠使用'React.createClass'。沒有使用插件。 React和ReactDOM都包含在內,因爲[在React的新版本中進行了此更改](https://facebook.github.io/react/blog/2015/10/07/react-v0.14.html#two-包 - 反應 - 和 - 反應-DOM)。 React用於創建類,ReactDOM用於實際渲染它。 –

+0

出於好奇,是不是封閉的功能問題,@Viuu -a?還是在關閉它們之後仍然存在? –