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

More than 1 year has passed since last update.

Twitter clone Part6 右側にニュース欄を加える

Posted at

概要

今回は右側にニュース欄を加えます。
News APIからニュース情報を取得
「Show more」ボタンを押すと表示するニュース件数を3件増やす

newsaaaaaaaaaaa.gif

開発環境

OS:Windows10
IDE:VSCode

package.json
{
  "name": "",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@heroicons/react": "^1.0.6",
    "next": "12.0.9",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },
  "devDependencies": {
    "@types/node": "^20.4.2",
    "@types/react": "^18.2.15",
    "autoprefixer": "^10.4.2",
    "eslint": "8.8.0",
    "eslint-config-next": "12.0.9",
    "postcss": "^8.4.5",
    "tailwindcss": "^3.0.18"
  }
}

実装のポイント

NewsAPIでデータ取得してブラウザ上で表示

image.png

show moreボタンを押すと表示件数が+3される 表示件数の変数をuseEffectにする

image.png

コード部分

News

News.jsx
export default function News({article}){
    return(
        <a href={article.url} target="_blank">
            <div className="flex items-center justify-between px-4 py-2 space-x-1 hover:bg-gray-200 transition duration-200">
                <div className="space-y-0.5">
                    <h6 className="text-sm font-bold">{article.title}</h6>
                    <p className="text-xs font-medium text-gray-500">{article.source.name}</p>
                </div>
                <img className="rounded-xl" width="70" src={article.urlToImage} alt="" />
            </div>
        </a>
    )
}

Widgets

Widgets.jsx

import { SearchIcon } from "@heroicons/react/outline";
import News from "./News";
+ import { useState } from "react";

+ export default function Widgets({ newsResults }) {
+ const [articleNum, setArticleNum] = useState(3);
  return (
    <div className="xl:w-[600px] hidden lg:inline ml-8 space-y-5">
      <div className="w-[90%] xl:w-[75%] sticky top-0 bg-white py-1.5 z-50">
        <div className="flex items-center p-3 rounded-full bg-red-300 relative">
          <SearchIcon className="h-5 z-50 text-gray-500" />
          <input
            className="absolute inset-0 rounded-full pl-11 border-gray-500 text-gray-700 focus:shadow-lg focus:bg-white bg-gray-100 "
            type="text"
            placeholder="Search Twitter"
          />
        </div>
      </div>

+      <div className="text-gray-700 space-y-3 bg-gray-100 rounded-xl pt-2 w-[90%] xl:w-[75%]">
+        <h4 className="font-bold text-xl px-4">What's happening</h4>
+        {newsResults.slice(0, articleNum).map((article) => (
+          <News key={article.title} article={article} />
+        ))}
+        <button onClick={()=>setArticleNum(articleNum + 3)}  className="text-blue-300 pl-4 pb-3 hover:text-blue-400">
+          Show more
+        </button>
+      </div>
    </div>
  );
}

page/index

index.jsx

import Head from 'next/head'
import Image from 'next/image'
import Sidebar from '../components/Sidebar';
import Feed from '../components/Feed';
import Widgets from '../components/Widgets';


- export default function Home() {
+ export default function Home({ newsResults }) {
  return (
    <div>
      <Head>
        <title>Twitter Clone</title>
        <meta name="description" content="Generated by create next app" />
        <link rel="icon" href="/favicon.ico" />
      </Head>
-      <main className="flex min-h-screen max-w-7xl mx-auto">
+      <main className="flex min-h-screen mx-auto">
        {/* Header */}

        {/* Sidebar */}
        <Sidebar />


        {/* Feed */}
        <Feed/>
        {/* Modal */}
-        <Widgets />
+        <Widgets newsResults={newsResults.articles} />
      </main>
    </div>
  )
}

+ export async function getServerSideProps() {
+  const newsResults = await fetch(
+    "https://saurav.tech/NewsAPI/top-headlines/category/business/us.json"
+  ).then((res) => res.json());
+  return {
+    props: {
+      newsResults,
+    },
+  };
}

参考

css

javascript

Next.js

SSR

image.png

React

firebase

その他

NewAPI top-headline

パラメーター

image.png

レスポンス(Postmanで確認)

image.png

Udemy

  1. Create the news section of the widgets component

githubコミット分

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