1
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?

Error: Objects are not valid as a React child の解決方法

Posted at

概要

Reactを勉強中、map関数で少しつまずきました。
初歩の初歩ですが、解決方法を記載します。

エラー内容

Objects are not valid as a React child (found: object with keys {title, time}). If you meant to render a collection of children, use an array instead.

コード

import "./styles.css";

const records = [
  { title: "勉強の記録1", time: 1 },
  { title: "勉強の記録2", time: 3 },
  { title: "勉強の記録3", time: 5 },
];

export default function App() {
  return (
    <div className="App">
      <h1>学習記録一覧</h1>
      {records.map((record) => (
        <p>{record}</p>
      ))}
      ;
    </div>
  );
}

原因

record内のkeyを指定できていなかったのが原因です。

解決策

このように指定してあげればOK

<p>
{record.title}
{record.time}時間
</p>

1
0
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
1
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?