英文:
React Ts trying to create guarded routed but getting an error
问题
抱歉,你提供的内容中包含了代码部分,但你要求我不要翻译代码。以下是你要求翻译的非代码部分:
Hi I tried creating guards for my routes but I am getting this error:
[Error] Error: [GuardedRoute] is not a
This is my code:
GuardedRoute.tsx-
import { Route, Navigate } from 'react-router-dom';
import { useUserInfo } from '../context/UserContext';
export const GuardedRoute = ({ path, element: Element, ...rest }: any) => {
const { userInfo } = useUserInfo();
if (userInfo.id !== '') {
return <Route path={path} element={Element} {...rest} />;
} else {
return <Navigate to="/" replace />;
}
};
App.tsx
<BrowserRouter>
<Nav></Nav>
<div className="w-[100vw] fixed top-[50px] z-[100] flex flex-col">
{
ErrorMessages.map((message, index) => (
<div className="w-[300px] h-[50px] mt-[10px] mx-auto bg-red-600/90 rounded-md grid place-content-center" key={index}>
<p className="font-bold text-gray-200 text-center">{message}</p>
</div>
))
}
</div>
<Routes>
<Route path="/" element={<Home />} />
<GuardedRoute path="/profile/:select?" element={<Profile />} />
<GuardedRoute path="/checkout" element={<CheckOut />} />
<Route path="/register" element={<Register />} />
<Route path="/login" element={<Login />} />
<Route path="/browse" element={<Browse />} />
<Route path="/product/:id" element={<Product />} />
<Route path="/404" element={<PageNotFound />} />
<Route path="*" element={<Navigate to="/404" />} />
</Routes>
</BrowserRouter>
Does anyone have a solution for this error? thanks.
英文:
Hi I tried creating guards for my routes but I am getting this error:
[Error] Error: [GuardedRoute] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>
This is my code:
GuardedRoute.tsx-
import { Route, Navigate } from 'react-router-dom';
import { useUserInfo } from '../context/UserContext';
export const GuardedRoute = ({ path, element: Element, ...rest }: any) => {
const { userInfo } = useUserInfo();
if (userInfo.id !== '') {
return <Route path={path} element={Element} {...rest} />;
} else {
return <Navigate to="/" replace />;
}
};
App.tsx
<BrowserRouter>
<Nav></Nav>
<div className="w-[100vw] fixed top-[50px] z-[100] flex flex-col">
{
ErrorMessages.map((message,index)=>(
<div className="w-[300px] h-[50px] mt-[10px] mx-auto bg-red-600/90 rounded-md grid place-content-center" key={index}>
<p className="font-bold text-gray-200 text-center">{message}</p>
</div>
))
}
</div>
<Routes>
<Route path="/" element={<Home />} />
<GuardedRoute path="/profile/:select?" element={<Profile />} />
<GuardedRoute path="/checkout" element={<CheckOut />}/>
<Route path="/register" element={<Register />} />
<Route path="/login" element={<Login />} />
<Route path="/browse" element={<Browse />} />
<Route path="/product/:id" element={<Product />} />
<Route path="/404" element={<PageNotFound />}/>
<Route path="*" element={<Navigate to="/404" />}/>
</Routes>
</BrowserRouter>
Does anyone have a solution for this error? thanks.
答案1
得分: 1
最好将所有私有路由放在 Authguard 中,而不是为每个组件提供 Authguard。您可以自定义身份验证,例如,如果特定用户存在 cookie,则身份验证为 true,否则为 false。
import { Outlet, Navigate } from 'react-router-dom';
const Authguard = () => {
let auth = false;
return (
auth ? <Outlet/> : <Navigate to="/" />
)
}
export default Authguard
现在,您的路由将如下所示,例如在 App.tsx 中:
function App(){
return(
<Routes>
<Route path="/" element={<Login />} />
<Route element={<Authguard />}>
<Route path="/register" element={<Register />} />
<Route path="/home" element={<Home />} />
</Route>
</Routes>
)
}
这显示您的注册和主页组件是私有的,而登录组件是公共的。
英文:
It is better to put all the private routes inside Authguard instead of providing Authguard to each component. you can customize the auth, like if there is cookie exists of particular user, then auth is true, otherwise false.
import { Outlet, Navigate } from 'react-router-dom';
const Authguard = () => {
let auth = false;
return(
auth ? <Outlet/> : <Navigate to="/"/>
)
}
export default Authguard
Now you Routes will be like, e.g in App.tsx
function App(){
return(
<Routes>
<Route path="/" element={<Login />} />
<Route element={<Authguard />}>
<Route path="/register" element={<Register />} />
<Route path="/home" element={<Home />} />
</Route>
</Routes>)
}
This shows your Register and Home components are private, and Login component is public.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论