Reactjs 如何在某些特定页面上不显示按钮?
新的反应在这里,我如何不显示按钮在某些特定的多页。这些页面是Reactjs 如何在某些特定页面上不显示按钮?,reactjs,react-redux,react-router,react-hooks,Reactjs,React Redux,React Router,React Hooks,新的反应在这里,我如何不显示按钮在某些特定的多页。这些页面是/login,/signup,忘记密码 App.js const-App=props=>{ 让路线=( Navbar.js const-Navbar=props=>{ 返回( ) 我所尝试的: {window.location.pathname='/login'-空: } 但是这个方法只能在一个页面上使用。不使用window.location.pathname而使用react router dom方法props.history.l
/login
,/signup
,忘记密码
App.js
const-App=props=>{
让路线=(
Navbar.js
const-Navbar=props=>{
返回(
)
我所尝试的:
{window.location.pathname='/login'-空:
}
但是这个方法只能在一个页面上使用。不使用window.location.pathname而使用react router dom方法props.history.location.pathname,您可以编写一个函数来检查是否包含路径名,如下所示
// this function accepts two arguments , one is string and an array , array contains all the routes that need to be compared to and it will return a boolean value , which can be used to render the component
function checkIfPathExist(pathname="",checkArrayRoute=[]){
if(checkArrayRoute.includes(pathname)){
return true
}
return false
}
在组件中,您可以执行以下操作
{checkIfPathExist(window.location.pathname,['/login']) ? null : <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"/>
</button>}
{checkIfPathExist(window.location.pathname,['/login'])空:
}
或
{checkIfPathExist(props.history.location.pathname,['/login'])空:
}
检查此沙箱以获取参考
不使用window.location.pathname使用react router dom方法props.history.location.pathname,您可以编写一个函数来检查是否包含路径名,如下所示
// this function accepts two arguments , one is string and an array , array contains all the routes that need to be compared to and it will return a boolean value , which can be used to render the component
function checkIfPathExist(pathname="",checkArrayRoute=[]){
if(checkArrayRoute.includes(pathname)){
return true
}
return false
}
在组件中,您可以执行以下操作
{checkIfPathExist(window.location.pathname,['/login']) ? null : <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"/>
</button>}
{checkIfPathExist(window.location.pathname,['/login'])空:
}
或
{checkIfPathExist(props.history.location.pathname,['/login'])空:
}
检查此沙箱以获取参考
我正在写一个沙盒,我会在完成后将它添加到上面的问题中。这也适用于“/signup”和其他页面?是的,给我5分钟,我会添加沙盒,你可以更好地理解我的意思!非常感谢我添加了沙盒,你可以参考它,在老虎和狮子的文件中,我看到了我添加了一个功能来检查条件路由,你也可以这样做,通过控制台检查该组件是否获得道具。历史记录,或者你可以用路由将其包装。我正在编写一个沙盒,我将在完成后将其添加到上述问题中这也适用于“/signup”和其他页面?是的,给我5分钟,我会dd sandbox,你可以更好地了解你!感谢它!我添加了沙盒,你可以参考它,在Tiger和Lion文件中,我添加了一个功能来检查条件路由,你也可以这样做,通过控制台检查组件是否获得道具。历史记录,或者你可以用withRout包装它呃
{checkIfPathExist(window.location.pathname,['/login']) ? null : <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"/>
</button>}
{checkIfPathExist(props.history.location.pathname,['/login']) ? null : <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"/>
</button>}