我正在try 从Reaction路由5迁移到Reaction路由6.我编写了一个简单的前端,只有在单击登录按钮时才能转到配置文件页面.在个人资料页面中,有两个链接可以查看个人资料和编辑个人资料.

//App.js
import './App.css';

import Header from './components/Header';
import HomePage from './pages/HomePage';
import AboutPage from './pages/AboutPage';
import Profile from './pages/Profile';
import NotFoundPage from './pages/NotFoundPage';
import Post from './pages/Post';
import GroupProfile from './pages/GroupProfile';

import {BrowserRouter as Router, Routes, Route, Navigate} from 'react-router-dom';
import {useState} from 'react';

function App(){
    const [login, setLogin] = useState(false);
    const [glogin, setGlogin] = useState(false);
    return(
        <div className="App">
            <Router>
                <Header />
                <button onClick={() => setLogin(!login)}> {!login?"Login":"Logout"} </button>
                <span> | </span>
                <button onClick={() => setGlogin(!glogin)}> {!glogin?"GroupLogin":"GroupLogout"} </button>
                <Routes>
                    <Route path='/' element={<HomePage />} />
                    <Route path='/about' element={<AboutPage />} />
                    {/*<Route path='/prof'>
                        {login ? <Profile />:<Navigate to='/' />}  Nope, in react_router_dom6 putting other things rather than function that returns pages are not valid
                    </Route>*/}
                    <Route path='/group' element={<GroupProfile g_login={glogin} />} />
                    <Route path='/prof/*' element={<Profile l_ogin={login} />} />
                        

                    <Route path='/post/:id' element={<Post />} />
                    <Route path='*' element={<NotFoundPage />} />

                </Routes>
            </Router>
        </div>
    )
}

export default App;

这两个页面是群组个人资料页面和个人资料页面.

GroupProfile.js Profile.js

import React from 'react';
import {useEffect} from 'react';
import {useNavigate} from 'react-router-dom';

function GroupProfile({ g_login }){
    let nav = useNavigate();
    useEffect(() => {
        if (!g_login) {
            nav("/");
        }
    },[g_login, nav])  //you know useEffect hook, if dependancy list is given, when they change then hook will be triggered.

    return(
        <div>
            <p>This is the GroupProfile</p>
        </div>
    )
}

export default GroupProfile;

Profile.js Profile.js

import React, {useEffect} from 'react';
import {Route, Routes, useRouteMatch, Link, useNavigate, useLocation} from 'react-router-dom';

import EditProfile from '../components/EditProfile';
import ViewProfile from '../components/ViewProfile';

/*
function Profile(){
    const { path, url } = useRouteMatch();
    //console.log(useRouteMatch());

    return(
        <>
            <h3>Profile Page</h3>
            <ul>
                <li><Link to={`${url}/viewprofile`}>View Profile</Link></li>
                <li><Link to={`${url}/editprofile`}>Edit Profile</Link></li>
            </ul>
            <Route path={`${path}/viewprofile`} component={ViewProfile} />
            <Route path={`${path}/editprofile`} component={EditProfile} />
        </>
    )
}
*/
function Profile({l_ogin}){
    let nav = useNavigate();
    let location = useLocation();
    //console.log(location)
    useEffect(() => {
        if (!l_ogin) {
            nav("/");
        }
    },[l_ogin, nav])  //you know useEffect hook, if dependancy list is given, when they change then hook will be triggered.
    return(
        <div>
            <h1>Congrats! you made it to the profile page mate...</h1>
            <ul>
                <li><Link to={`${location}/viewprofile`}>View Profile</Link></li>
                <li><Link to={`${location}/editprofile`}>Edit Profile</Link></li>
            </ul>
            <Routes>

                <Route path={`${location}/viewprofile`} element={<ViewProfile />} />
                <Route path={`${location}/Editprofile`} element={<EditProfile />} />
            </Routes>
        </div>
    )
}

export default Profile;

I have commented the Profile component that works in react router version 5 and it is supposed to navigate to /prof/viewprofile when View Profile link is clicked. ViewProfile.js Profile.js and EditProfile.js Profile.js are simple components

ViewProfile.js Profile.js

import React from "react";

const ViewProfile = () => {
    return(
        <div>
            <h3><i>This is what you have to see in the profile.</i></h3>
        </div>
    )
}

export default ViewProfile;

我在Reaction路由5中使用的代码运行良好,当我点击查看配置文件时,它会导航到/prof/viewprofile,没有任何问题.但是,当我使用REACT路由6时,它不会导航到/prof/viewprofile,它只是停留在/prof中,但url更改为类似/prof/[object%20Object]/viewprofile的内容.有人能告诉我我哪里做错了吗?

推荐答案

一个对象,如果这是你试图使用的,你需要location.pathname,但这是104,你想要try 并使用它来构建后代路由的路径.当呈现Routes组件和派生路由时,这些路由已经构建105到父路由.没有必要自己try 构建/实现这种相对性.

示例:

function Profile({ l_ogin }){
  const navigate = useNavigate();

  useEffect(() => {
    if (!l_ogin) {
      navigate("/", { replace: true });
    }
  }, [l_ogin, navigate]);

  return(
    <div>
      <h1>Congrats! you made it to the profile page mate...</h1>
      <ul>
        <li><Link to="viewprofile">View Profile</Link></li>
        <li><Link to="editprofile">Edit Profile</Link></li>
      </ul>
      <Routes>
        <Route path="viewprofile" element={<ViewProfile />} />
        <Route path="editprofile" element={<EditProfile />} />
      </Routes>
    </div>
  );
}

如果您希望保留您try 使用的第一个路由实现,则这是正确的语法.您仍然需要在elementprops 上渲染传送的内容.

<Route
  path='/prof'
  element={login ? <Profile /> : <Navigate to='/' replace />}
/>

Reactjs相关问答推荐

如何防止使用useCallback和memo重新渲染

react 派生状态未正确更新

如何在没有 node 模块的情况下利用外部Java脚本文件制作OWL carousel

如何使Reaction Select 选项菜单从选项数组的特定索引开始

有没有办法将在服务器端全局获取的一些数据传递到Next&>13应用程序目录中的客户端组件?

获取点击的国家/地区名称react 映射

折叠并重新打开react 手风琴将重置内部状态

如何在物料界面react 的多选菜单中设置最大 Select 数限制

使用Ionic 7和React 18,如何访问历史对象以在页面之间导航?

React组件未出现

使用Next.js和Next-intl重写区域设置

useState数组不更新

React 应用程序可以嵌入到 PowerPoint 中吗?

useEffect 渲染没有依赖数组的组件

next js 13 在获取中使用标头时动态渲染而不是静态渲染?

useState 在生命周期中的位置

自动重新获取不起作用 - RTK 查询

如何根据数据库中的值设置单选按钮选中? - react

将鼠标悬停在仅适用于该类的第一个实例的 p5.js 类上

Material UI 安装和 React v. 18.2 出现问题