鈍足ランナーのIT日記

走るのが好きな5流のITエンジニアのブログ。

趣味の範囲は広いけど、どれも中途半端なクソブロガー楽しめるWebアプリを作ってあっと言わせたい。サーバーサイドPerl(Mojolicious)、クライアントサイドVue.js。Arduinoにも触手を伸ばす予定。

this.stateに値が設定できないーー。 undefinedなものに入れようとしていた

class IndexSidebar extends React.Component {

  constructor(props){

    super(props);
    
    console.log("IndexSidebar context");
    console.log(this.props.context.userStore.getUserData());

    this.state.userStore = this.props.context.userStore; // NG
    
    this.userStore = this.props.context.userStore; // OK

    this.state = { userStore: this.props.context.userStore}; // OK
  }


Uncaught TypeError: Cannot set property 'userStore' of undefined
となっていて発狂しそうになっていたら

stackoverflow.com


まだ、constructorの中ではstateが初期化されていないということか。

これ分かるのに、どれだけ時間がかかっているんだろう。自己嫌悪に。。