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?

Reactでトップページを変更するには

Posted at

reactでトップページを変更する方法について

プロジェクトの作成

まず作業の土台となるプロジェクトを作成します。

npx create-react-app プロジェクト名 
cd my-app

必要機能のインストール

下記の機能をインストールします

npm install react-router-dom

これで諸準備が整いました。

App.jsの編集

基本的にReactにおいて初期状態でApp.jsがトップページとなっています。

下記のようにApp.jsの⓵⓶⓷を編集することでHomePage.jsがトップページとなります。

App.js
import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
// ⓵ルーティング機能の追加
import HomePage from './HomePage';
// ⓶トップページコンポーネントの追加
// pagesフォルダ内に設定した場合
// import HomePage from './pages/HomePage';となります

function App() {
    return (
        <Router>
            <Routes>
                <Route path="/" element={<HomePage />} />       
                {/* ⓷トップページへのルーティング */}
            </Routes>
        </Router>
    );
}

export default App;

下記のように設定すれば完成です!

Homepage.js
import React from 'react';

function HomePage() {
    return (
        <div>
            <h1>トップページへようこそ!</h1>
        </div>
    );
}

export default HomePage;
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?