Reactjs 在React中获取可选参数
我有下面的路Reactjs 在React中获取可选参数,reactjs,react-router,react-dom,Reactjs,React Router,React Dom,我有下面的路 <Route path='/All/Profiles/:username?' component={ProfileLookupPage} /> 在ProfileLookupPage组件中,如何获取所有可选参数。例如,可以是Profile ProfileLookupPage.js中的/all/profiles或/all/profiles/abc您可以使用params或props.match.params.username import {us
<Route path='/All/Profiles/:username?' component={ProfileLookupPage} />
在
ProfileLookupPage
组件中,如何获取所有可选参数。例如,可以是Profile ProfileLookupPage.js中的/all/profiles
或/all/profiles/abc
您可以使用params或props.match.params.username
import {useParams} from "react-router-dom"
function ProfileLookupPage(props){
const {username}=useParams();
or
console.log(props.match.params.username,"username")
return {
<></>
}
}
从“react router dom”导入{useParams}
功能配置文件lookuppage(道具){
const{username}=useParams();
或
console.log(props.match.params.username,“username”)
返回{
}
}
如果我没弄错你的问题,你可以在你的档案中使用const{username}=useParams()
你在使用react路由器吗?如果是这样,请尝试阅读文档。