2016-08-30 5 views
0

私は反応するために新しいです、私はちょうど様々なdivsのためのビューカウンタを作っています。私はdivとscrollTopの高さの高さが必要です。しかし、値を取得した後、私は値をsetstateする必要があります。あなたはちょうどここに構文エラーがあり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'条件が成り立つかどうかチェックしましたか? 'nullh == undefined // true'を使用している場合)条件を使用して' setState'が呼び出されることはありません – Igorsvee

答えて

0

、JSX周りの単一引用符を削除 -

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

ここで作業コピーを参照してください - https://jsfiddle.net/69z2wepo/54531/

+0

申し訳ありませんが、私はしませんでしたミスをしました。私はStackOverflowで投稿したときに何とか変更しました。私の実際のコードでは引用符はありません。でも私の状態を更新することはできません。 – Nane

+0

@ネーン次に問題はどこに見えませんか?あなたが 'document.getElementById( 'newscontanier')。children'を見れば、それらのdiv項目がすべてあり、その情報がそこにありますか?あなたが提供されたフィドルを見れば、あなたの状態は現在5に更新されています。あなたの全体的な質問は不明確です。私もあなたが 'state.length'を見ている理由を知りません、' state.news.length'を意味しましたか? – ajmajmajma

関連する問題