- 쇼핑몰 사이트 프로젝트
$ npm init vite
$ npm install
src
├─ assets
├─ components
├─ pages
├─ hooks
└─ store
import './App.css';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import Layout from './components/layout/Layout';
import HomePage from './pages/HomePage';
import DetailPage from './pages/DetailPage';
import CartPage from './pages/CartPage';
import LoginPage from './pages/LoginPage';
import RegisterPage from './pages/RegisterPage';
import OrderPage from './pages/OrderPage';
import NotFoundPage from './pages/NotFoundPage';
const App = () => {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<HomePage />} />
<Route path="product/:id" element={<DetailPage />} />
<Route path="cart" element={<CartPage />} />
<Route path="login" element={<LoginPage />} />
<Route path="register" element={<RegisterPage />} />
<Route path="order" element={<OrderPage />} />
<Route path="*" element={<NotFoundPage />} />
</Route>
</Routes>
</BrowserRouter>
);
};
export default App;
Outlet
을 넣어줘야 함.import styles from './Layout.module.scss';
import { Outlet } from 'react-router-dom';
import Header from '../header/Header';
import Footer from '../footer/Footer';
const Layout = () => {
return (
<div className={styles.layout}>
<Header />
<Outlet />
<Footer />
</div>
);
};
export default Layout;
import styles from './Header.module.scss';
import { Link } from 'react-router-dom';
import Nav from './nav/Nav';
const Header = () => {
return (
<div className={styles.header}>
<div className="container">
<div className={styles.header_wrapper}>
<div className={styles.header_logo}>
<Link to={'/'}>
<h2>Shop</h2>
</Link>
</div>
<Nav />
</div>
</div>
</div>
);
};
export default Header;
{...register("name", validCheckObject)}
const { register, handleSubmit, formState: {errors}, reset } = useForm();
const onSubmit = ({email, password}) => {
console.log(email, password);
}
// 유효성 검사
const userEmail = {
required: "필수 값입니다.",
minLength: {
value: 4,
message: "최소 4자입니다."
},
pattern: {
value: /^~~~~~~$/gm,
message: '최소 8자, 영문 1자, 숫자 1자.'
}
}
const userPassword = {
// 동일
}
<form onSubmit={handleSubmit}>
<input {...register("email", userEmail} />
<span>{errors?.email && errors.email.message}</span>
<input {...register("password", userPassword} />
<span>{errors.password && errors.password.message}</span>
</form>
const { register, handleSubmit, formState: {errors}, reset } = useForm({
mode: 'onBlur'
});
-> onBlur, onChange, onSubmit 3개가 있다. (기본값: onSubmit)