componentDidMount没有更新我的状态?

时间:2016-08-30 17:29:49

标签: javascript jquery reactjs

我是新手做出反应,我只是为各种div制作一个视图计数器。因为我需要div和scrollTop高度的高度。但是在得到那些值后我必须设置值。但是它不起作用。

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 :(得分:0)

这里只有语法错误,删除jsx周围的单引号 -

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

请在此处查看工作副本 - https://jsfiddle.net/69z2wepo/54531/