Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在React中获取可选参数_Reactjs_React Router_React Dom - Fatal编程技术网

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路由器吗?如果是这样,请尝试阅读文档。