Let's take a look at the main components of React Router.
The main components of React Router can be divided into 3 main components. BrowserRouter that acts as a router, Switch & Route that matches routes, and Link that changes routes.
To use these components, you need to load them seperately from the React Router Library.
import { BrowserRouter, Switch, Route, Link } from "react-router-dom"
** Import is the role of importing required modules and can be used simliarly to destucturing assignment.
import logo from './logo.svg';
import './App.css';
import React from 'react'
import { BrowserRouter, Switch, Route, Link } from "react-router-dom";
function App() {
return (
<BrowserRouter>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link> {/* Link 컴포넌트를 이용하여 경로를 연결합니다 */}
</li>
<li>
<Link to="/about">MyPage</Link>
</li>
<li>
<Link to="/dashboard">Dashboard</Link>
</li>
</ul>
</nav>
{/* 주소경로와 우리가 아까 만든 3개의 컴포넌트를 연결해줍니다. */}
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/about"> {/* 경로를 설정하고 */}
<MyPage /> {/* 컴포넌트를 연결합니다. */}
</Route>
<Route path="/dashboard">
<Dashboard />
</Route>
</Switch>
</div>
</BrowserRouter>
);
}
// Home 컴포넌트
function Home() {
return <h1>Home</h1>;
}
// MyPage 컴포넌트
function MyPage() {
return <h1>MyPage</h1>;
}
// Dashboard 컴포넌트
function Dashboard() {
return <h1>Dashboard</h1>;
}
export default App;
Take a string as input and return a string in which the first letter of each word composing the string is an uppercase letter.
function letterCapitalize(str) {
const arr = str.split(" ");
for(i = 0; i < arr.length; i++){
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substr(1);
}
const result = arr.join(" ");
return result;
}