我是新來的反應,我只是製作一個查看計數器的各種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