0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【React】TypeError: Cannot read property '***' of undefined の対処法

Posted at

今回は練習のためにreactでTodoリストを作成していた時にハマったエラーと対処法をメモしときたいと思います。

TypeError: Cannot read property '***' of undefined

これはリストの削除機能を作る時にハマりました。

そのまま翻訳すると「未定義のプロパティ「***」を読み取れません」といった感じなんですが、これはちゃんと定義されていないことが問題なようなのでアロー関数で書いてあげれば治りました。

test.js
removeTask(text){
        var updatedTasks = this.state.tasks;
        updatedTasks.splice(updatedTasks.indexOf(text), 1);
        this.setState({tasks: updatedTasks});
}

これを以下に変更。

test.js
removeTask=(text)=>{
        var updatedTasks = this.state.tasks;
        updatedTasks.splice(updatedTasks.indexOf(text), 1);
        this.setState({tasks: updatedTasks});
}

こんな感じでちゃんと渡してあげたらスッと治りました。

Cannot read property 'map' of undefined

このエラーはほとんど普通にタイプミスです。
私の場合は「var tasksList=[];」を「tasks=tasksList」に定義し直していたのを忘れて「tasksList」で進めてしまっていたのでエラーが出たといった感じでした。

まとめ

どちらもしょうもないミスなんですが、結構ハマった時間が長かったのでもし困っている方の助けになれたら嬉しいです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?