简体   繁体   English

如果 url 错误,我需要你重定向到 Shop。 反应 js

[英]I need you to redirect to Shop if url is wrong. React js

I need it to redirect to my main page if the url is wrong, the problem must be in this part, because before I used Redirect, but in the new version of React it is Navigate and I don't know if the way of entering changed如果url错误,我需要它重定向到我的主页,问题肯定出在这部分,因为我之前使用Redirect,但在新版本的React中是Navigate,我不知道进入方式是否正确变了

import React from 'react';
import {Routes, Route, Navigate} from 'react-router-dom';
import {authRoutes, publicRoutes} from '../routes';
import { SHOP_ROUTE } from '../utils/consts';

const AppRouter = () => {
    const isAuth = false
    return (
        <Routes>
            {isAuth && authRoutes.map(({path, Component}) =>
                <Route key={path} path={path} element={<Component/>} exact/>
            )}
            {publicRoutes.map(({path, Component}) =>
                <Route key={path} path={path} element={<Component/>} exact/>
            )}
            <Navigate to={SHOP_ROUTE}/>
        </Routes>
    );
};

export default AppRouter;

If you include a <Route path="*" /> component (as shown in the docs ) at the end of the <Routes /> , then the last route will be shown when no other route has been matched.如果您在<Routes />的末尾包含一个<Route path="*" />组件(如文档中所示),那么当没有其他路由匹配时,将显示最后一条路由。

import React from 'react';
import {Routes, Route, Navigate} from 'react-router-dom';
import {authRoutes, publicRoutes} from '../routes';
import { SHOP_ROUTE } from '../utils/consts';

const AppRouter = () => {
    const isAuth = false
    return (
        <Routes>
            {isAuth && authRoutes.map(({path, Component}) =>
                <Route key={path} path={path} element={<Component/>} exact/>
            )}
            {publicRoutes.map(({path, Component}) =>
                <Route key={path} path={path} element={<Component/>} exact/>
            )}
            <Route path="*" element={<NotFound />} />
        </Routes>
    );
};

export default AppRouter;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你好,我在 discord 模态中收到一个错误,它告诉我出了点问题。 再试一次 discord.js - hello I am getting an error that shows up in discord modal it tells Something went wrong. try again discord.js 我一直把这个问题弄错了。 使用 javascript 计数位 - I keep getting this question wrong. Counting Bits using javascript 当该网址不允许在iframe中时,我需要重定向到该网址? - I need to redirect to a url, when the url not allowed to be inside a iframe? 有没有一种方法可以让我在响应URL之前将其重定向到其他URL? - Is there a way i can get the url in react before it redirect to other url? React JS - 带数据切换的链接不会重定向到url - React JS - Link with data-toggle doesn't redirect to url React.js:State 在 url 重定向之前没有得到更新 - React.js: State not getting update before url redirect 我需要在 JS 中的 url 旁边添加 HTML 的“选择/选项”的“值”。 你知道怎么做吗? - I need to add “value” of the “select / option” of HTML next to the url that is in the JS. Do you have an idea how to do it? 在反应中重定向 url - Redirect url in react 我究竟做错了什么。 我想显示没有时间戳的日期但添加 50 天 - What am I doing wrong. I want to display the date without timestamp but add 50 days 关于“索引”,我认为是对的,错的是对的。 我不知道为什么 - About the “index”,i think it is right, when it is wrong. i do not know why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM