Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- i 태그
- html tag i
- reveal in file explorer
- go live
- tag html
- Checked 의사 클래스
- not 의사클래스
- padding 속성
- br 태그
- 아두이노
- Live Server
- css
- focus 의사클래스
- 전체 선택자
- RGBA
- id 선택자
- 임베디드
- height속성
- 일반 형제 결합자
- sup태그
- RGB
- width속성
- 자식결합자
- sub태그
- 인접 형제 결합자
- iframe
- iframe 태그
- html
- background-color 속성
- html 태그
Archives
- Today
- Total
so woon!
[React] React Router2 본문
학습일 : 2023. 03. 20
<Switch> 컴포넌트는
한번의 하나의 Route만 렌더링 시켜주게 된다.
먼저, 두개의 Route를 생성해 볼 것이다.
1.
첫번째 Route는 Home 컴포넌트를 보여줄 것이다.
유저가 홈화면에 있을때 즉, 경로가 localhost:3000에 머물러 있을 때
우리는 유저에게 Home Route를 렌더링 해줄 것이고,
Home 컴포넌트를 보여주게 될 것이다.
사용방법은
<Route> 안에 path="/" 라고 써주면 된다.
localhost:3000의 실행결과
2.
두번째 Route는 Detail컴포넌트를 보여줄 것이다
유저의 경로가 localhost:3000/movie 에 있을 때 보여줄 컴포넌트이므로
<Route path="/movie">
<Detail />
</Route>
이렇게 작성해주면 된다.
localhost:3000/movie 의 실행결과
전체 코드
<App.js>
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Home from "./routes/Home";
import Detail from "./routes/Detail";
function App() {
return (
<Router>
<Switch>
<Route path="/movie">
<Detail />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</Router>
);
}
export default App;
<Detail.js>
function Detail() {
return <h1>Detail</h1>
}
export default Detail;
<Home.js>
import { useEffect, useState } from "react";
import Movie from "../components/Movie";
function Home() {
const [loading, setLoading] = useState(true);
const [movies, setMovies] = useState([]);
const getMovies = async () => {
const json = await (
await fetch(
`https://yts.mx/api/v2/list_movies.json?minimum_rating=9&sort_by=year`
)
).json();
setMovies(json.data.movies);
setLoading(false);
};
useEffect(() => {
getMovies();
}, []);
return (
<div>
{loading ? (
<h3>로딩 중...</h3>
) : (
<div>
{movies.map((movie) => (
<Movie
key={movie.id}
coverImg={movie.medium_cover_image}
title={movie.title}
summary={movie.summary}
genres={movie.genres}
/>
))}
</div>
)}
</div>
);
}
export default Home;
'ReactJS > 개념정리' 카테고리의 다른 글
[React] useParams() 사용하기 (0) | 2023.03.21 |
---|---|
[React] <Link></Link> 사용하기 (0) | 2023.03.20 |
[React] React Router1 (0) | 2023.03.20 |
[React] <component /> 사용하기 (0) | 2023.03.20 |
[React] 영화 정보 불러오기 (0) | 2023.03.20 |
Comments