3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Reactの削除処理を書いて間違えた話

3
Last updated at Posted at 2026-09-28

はじめに

  • 業務ではテストコードは自分で書いて、実装はかなりAIで書いている

問題

  • こんな感じかな?で書いて間違えた
  const deleteRecord = async  (id) => {
      const newRecords = records.map((record) => !record.id) // [true, false]
      setRecords(newRecords)
  }
  const deleteRecord = async  (id) => {
      const newRecords = records.map((record) => record.id !== id && record) // [{...}, false]
      setRecords(newRecords)
  }

解決

  • 検索してfilter使えばいいと思い出した
  const deleteRecord = async  (id) => {
      const newRecords = records.filter((record) => record.id !== id)
      setRecords(newRecords)
  }

終わりに

  • mapでfalsyな値を弾こうかなと検索して .Boolean() みたいな記事出てきたのが懐かしかった

3
0
2

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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?