2016-08-30 72 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