2017-07-17 10 views
0

単純な問題ですが、解決策を見つけられません。私は、期待どおりに動作するテキストボックスに単純な文字カウンタを持っていますが、私はconstructorcomponentWillReceivePropsに問題があります。エラー.length undefined.が表示されます。長さが定義されていない場合、これをどのように機能させるのですか?コンストラクタの要素が未定義でエラーを返します

constructor(props) { 
    super(props); 
    const profileCandidateCollection = props.profileCandidate; 
    const summary = profileCandidateCollection && profileCandidateCollection.summary; 
    const count = 3000 - (profileCandidateCollection && profileCandidateCollection.summary.length); 

    this.state = { 
    count: count, 
    }; 
} 

componentWillReceiveProps(nextProps) { 
    const profileCandidateCollection = nextProps.profileCandidate; 
    const summary = profileCandidateCollection && profileCandidateCollection.summary; 
    const count = 3000 - (profileCandidateCollection && profileCandidateCollection.summary.length); 

    this.setState({ 
    count: count, 
    }); 
} 
+0

エラーを再現するスニペットを追加できますか? – tugce

+0

'TypeError:プロパティ '長さ'が未定義です ' – bp123

答えて

0

働いているように見えるソリューションは、if文を使用してvarの賛成でconstを落としています。なぜそれが働くのか分かりません。

if(summary == null) { 
    var count = 0; 
    } else { 
    var count = 3000 - (profileCandidateCollection && profileCandidateCollection.summary.length); 
    } 

    this.setState({ 
    count: count 
    }); 
    } 
関連する問題