2016-08-30 39 views
0

我非常需要jQuery Api,但事情是我使用了React js的api。我只是混淆了我的React js生命週期。 附加信息我正在爲不同div創建視圖計數器。在我看來點我很喜歡jQuery的API來照顧高度,scrollTop的.. 下面的代碼將輸出未定義如何整合jQuery與React js?

var React = require('react'); 
var ReactDOM = require('react-dom'); 
var NewsDetail = require('./news-details'); 
var $ = require('jquery'); 


module.exports = React.createClass({ 
    getInitialState:function() { 

     return{ 
      news:[ 
       { 

       { 
        id:"5", 
        data:"I probably am using the wrong I probably am using the wrong terminology here but this is what I'm trying to do. I want to create a map with a value terminology here but this is what I'm trying to do. I want to create a map with a value for each key and a value for each key's object. S" 
       }, 
       { 
        id:"6", 
        data:"I probably am using the wrong I probably am using the wrong terminology here but this is what I'm trying to do. I want to create a map with a value I probably am using the wrong terminology here but this is what I'm trying to do. I want to create a map with a value I probably am using the wrong terminology here but this is what I'm trying to do. I want to create a map with a value terminology here but this is what I'm trying to do. I want to create a map with a value for each key and a value for each key's object. S" 
       }, 
       { 
        id:"7", 
        data:"I probably am using the wrong terminology here but this is what I'm trying to do. I want to create a map with a value I probably am using the wrong terminology here but this is what I'm trying to do. I want to create a map with a value I probably am using the wrong terminology here but this is what I'm trying to do. I want to create a map with a value for each key and a value for each key's object. S" 
       }, 
       { 
        id:"8", 
        data:"I probably am using the wrong I probably am using the wrong terminology here but this is what I'm trying to do. I want to create a map with a value terminology here but this is what I'm trying to do. I want to create a map with a value for each key and a value for each key's object. S" 
       }, 
       { 
        id:"9", 
        data:"I probably am using the wrong I probably am using the wrong terminology here but this is what I'm trying to do. I want to create a map with a value I probably am using the wrong terminology here but this is what I'm trying to do. I want to create a map with a value I probably am using the wrong terminology here but this is what I'm trying to do. I want to create a map with a value terminology here but this is what I'm trying to do. I want to create a map with a value for each key and a value for each key's object. S" 
       }, 

      ], 
      top:0, 
      height:null 
     } 
    }, 
    componentWillMount:function() { 

    }, 
    componentDidUpdate:function() { 

     var height = $('#newscontainer').height(); 
     alert(height)//udefined 

    }, 
    render:function() { 

     var newsdata = this.state.news.map(function (newss) { 

      return(<NewsDetail key={newss.id} {...newss}/>) 
     }); 
     return(
      <div className="newscontanier" id="newscontanier">{newsdata}</div> 
     ) 



    } 

}) 
+2

?如果你只是獲得元素的高度,你可以做到沒有jQuery。 –

+1

var height = $('#newscontainer')。height();根據您的DOM選擇錯誤的ID。 ... id =「newscontanier」 –

+0

您的ID在您的渲染功能中拼寫錯誤。 – ray

回答

1

你有錯字newscontanier。但是爲什麼你需要的jQuery,你可以做到這一點像

<div ref={elem => this.elem = elem} className="newscontanier" id="newscontanier">{newsdata}</div> 

,然後你可以找到DOM元素

this.elem.getDOMNode(); 
+0

getDOMNode is [deprecated](https: //facebook.github.io/react/blog/2015/10/07/react-v0.14.html#dom-node-refs) – ray

+0

@ray我不知道它。無論如何,你可以從'this.elem'獲得DOM節點 –