- vừa được xem lúc

Bài 4: Cài đặt thư viện react-router-dom và cấu hình router cho dự án Reactjs

0 0 32

Người đăng: Trần Minh Hiếu

Theo Viblo Asia

1. Cài đặt thư viện React-router-dom

yarn add react-router-dom

2. Tạo một số trang tương ứng với các router:

chúng ta sẽ tạo 3 pages: page Home: Không cần login vẫn có thể truy cập được ( pages/Home/index.tsx) page Profile: Cần login mới có thể truy cập được ( pages/Profile/index.tsx) page Login: Phục vụ cho việc login ( pages/Login/index.tsx) ** Trong folder src tạo folder pages**

  • Tạo trang Home
function Home() { return <h2>Home page</h2>;
} export default Home;
  • Tạo trang Profile
function Profile() { return <h2>Profile page</h2>;
} export default Profile;
  • Tạo trang Login
function Login() { return <h2>Login page</h2>;
} export default Login;

3. Cấu hình router và tạo privteRouter

** Trong folder src tạo folder routes và file index.ts**

import { Fragment } from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import { publicRoutes, privateRoutes } from '~/routes';
// import { DefaultLayout } from '~/components/Layout'; function App() { const isLogin = false; return ( <Router> <div className="App"> <Routes> {publicRoutes.map((route, index) => { const Page = route.component; // let Layout = DefaultLayout; // if (route.layout) { // Layout = route.layout; // } else if (route.layout === null) { // Layout = Fragment; // } return ( <Route key={index} path={route.path} element={ // <Layout> <Page /> // </Layout> } /> ); })} {privateRoutes.map((route, index) => { const Page = route.component; return ( <Route key={index} path={route.path} element={!isLogin ? <Navigate to="/login" /> : <Page />} /> ); })} </Routes> </div> </Router> );
} export default App;

Bình luận

Bài viết tương tự

- vừa được xem lúc

Tìm hiểu về React Router

React Router là một API cho các ứng dụng React. React router sử dụng định tuyến động phía máy khách cho phép xây dựng SPA (Single Page Application) với điều hướng mà không cần làm mới trang.

0 0 21

- vừa được xem lúc

Xây dựng một Single-page Application cơ bản với React Router DOM như thế nào ?

Mở đầu. Trong những website thông thường khi điều hướng từ page này sang page khác, chúng ta sẽ sử dụng thẻ <a> để làm điều đó, nhưng trong React JS thường được sử dụng để xây dựng nhữmg Single Page A

0 0 215

- vừa được xem lúc

Build React Admin Dashboard with Multiple Themes | React Admin Panel CSS | ReactJS

Build React Admin Dashboard with Multiple Themes | React Admin Panel CSS | ReactJS. . In this video, we will Build React Admin Dashboard with Multiple Themes. .

0 0 189

- vừa được xem lúc

React Ecommerce Web Shop | Part 01 - Homepage | ReactJS SASS

React Ecommerce Web Shop | Part 01 - Homepage | ReactJS SASS. . In this video, we will Build React Ecommerce Web Shop Homepage with SASS. .

0 0 406

- vừa được xem lúc

React Ecommerce Web Shop | Part 02 - Catalog and Product page| ReactJS SASS

React Ecommerce Web Shop | Part 02 - Catalog page and Product page| ReactJS SASS. . In this video, we will Build React Ecommerce Web Shop with SASS. .

0 0 102

- vừa được xem lúc

React Ecommerce Web Shop | Part 03 - Shopping cart | ReactJS SASS

React Ecommerce Web Shop | Part 03 - Shopping cart | ReactJS SASS. . In this video, we will Build React Ecommerce Web Shop with SASS. .

0 0 163