我是新来的反应,我只是制作一个查看计数器的各种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>)
}
})
我建议你学习一下就命名变量。看着你的代码,它非常难以找出发生了什么,以及是否有错误或故意。 –
对不起,先生,我对编程新手。代码实际上正确渲染div。但状态不更新。 – Nane
您是否检查过'lengthh!= this.state.length'条件是否成立? 'lengthh'变量可能是'undefined',并且条件使用非严格等号运算符(如果使用'null == undefined // true'),所以'setState'根本不会被调用 – Igorsvee