2016-08-30 74 views
0

我是新來的反應,我只是製作一個查看計數器的各種divs.For我需要div和scrollTop height.But的高度,但獲得後,我必須設置valuesstd值。但它不工作。componentDidMount沒有更新我的狀態?

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" 
      }, ], 
      length: null, 
      height: 0 
     } 
     }, 
     componentWillMount: function() {}, 
     componentDidMount: function() { 

     console.log(this.state.length) 
     var lengthh = document.getElementById('newscontanier').children.length; 
     console.log(lengthh); 
     if(lengthh != this.state.length) { 
      this.setState({ 
      length: lengthh //Still null 
      }) 
     } 
     }, 
     render: function() { 
     var newsdata = this.state.news.map(function(newss) { 
      return(< NewsDetail key = { 
       newss.id 
       } {...newss 
       } 
       />) 
      }); 
      return(<div className="newscontanier" id="newscontanier">{newsdata}</div>) 
     } 
     }) 
+1

我建議你學習一下就命名變量。看着你的代碼,它非常難以找出發生了什麼,以及是否有錯誤或故意。 –

+0

對不起,先生,我對編程新手。代碼實際上正確渲染div。但狀態不更新。 – Nane

+0

您是否檢查過'lengthh!= this.state.length'條件是否成立? 'lengthh'變量可能是'undefined',並且條件使用非嚴格等號運算符(如果使用'null == undefined // true'),所以'setState'根本不會被調用 – Igorsvee

回答

0

你只是有一個語法錯誤在這裏,除去周圍的JSX單引號 -

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

看到這裏的工作副本 - https://jsfiddle.net/69z2wepo/54531/

+0

對不起,先生我沒有犯了這個錯誤。當我在StackOverflow中發佈信息時,我改變了這種情況。在我的實際代碼中沒有引號。但仍然無法更新我的狀態 – Nane

+0

@Nane然後我看不到問題在哪裏?如果你只是看看'document.getElementById('newscontanier')。children'你有所有這些div項和他們的信息在那裏?如果你看提供的小提琴,你的狀態現在更新到5。你的整體問題是不清楚的。我也不知道你爲什麼要查看'state.length',你的意思是'state.news.length'嗎? – ajmajmajma