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 1 year has passed since last update.

Next.jsで配列内のJSONデータで必要な部分のみ出力したい

Last updated at Posted at 2022-05-22

はじめに

もっと良い方法があるかもしれないですが、調べた中で上手くいったコードをメモがてら記載

コード

index.js
const [qiita, setQiita] = useState('');
const fetchQiita = async () => {
  // データを取得
  const response = await fetch(`/api/qiita?query=${keyword}`);
  let qiita = await response.json();
 // 配列を1つ出力して別の変数に格納
  const listQiita = qiita.map(article => 
    <>
      <li><a href={article.url} target="_blank" rel="noopener noreferrer">{article.title}</a></li>
      <li>{article.body}</li>
    </>
  );
 // useStateの変数にセットする
  setQiita(listQiita);
};
---
// 以下{qiita}で画面に出力する
<div>
  <ul>
    {qiita}
  </ul>
</div>

参考

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?