【发布时间】:2022-07-16 13:18:37
【问题描述】:
我正在使用 react-router-dom No Match404 并且它工作正常但是当使用 useParams 转到路径 a 时,它会呈现 404 页面,代码如下:
{/* Here it works fine it renders the routes */}
<Route path='/' exact component={Home} />
<Route path='/help' component={Help} />
<Route path='/signup' component={Signup} />
{/* The 404 page that render when a path does not exists */}
<Route component={Page404} />
{/* but when I try to render the route with useParams which is the uid I get from firebase it just render the Page404 */}
<Route path='/user/:uid' component={Profile} />
{/*-----------------Profile page------------------------*/}
// heres the profile page that uses useParams
import { useParams} from 'react-router-dom'
import {auth} from './firebase/config'
function Profile() {
let { uid } = useParams();
// here I have a variable that get a signed in user Unique Id from firebase
const [userId, setUserId] = useState('')
auth.onAuthStateChanged((user) => {
if (user) {
// User logged in already or has just logged in.
setUserId(user.uid);
} else {
// User not logged in or has just logged out.
}
});
if (uid === userId ) {
return <div>This is your profile</div>
}else{
return <div>This is someone elses or it does not exist</div>
}
}
此代码在我删除 404 路由时运行良好,但当我放置它时,Profile 路由会呈现 404 页面。
有没有办法让 404 页面只在路由实际不存在时才呈现。
【问题讨论】:
-
您是否尝试将 404 路由置于所有其他路由之后。
-
嗨@JiaSH 谢谢你的回复,是的,我试过了还是不行
-
请分享一个更完整更全面的代码示例。你指的是什么
useParams钩子? stackoverflow.com/help/minimal-reproducible-example -
@DrewReese 感谢您的回复,我已经更新了问题。
标签: reactjs react-router react-router-dom