Home > other >  Reactjs hide navbar in all component have path = /admin/
Reactjs hide navbar in all component have path = /admin/

Time:10-22

i want to hide my navbar when i'm in all page have path='/admin/...' but it's so length. How can i clean it, thank very much.

let HideHeader = window.location.pathname === '/admin/info' && '/admin/setting' && '/admin/post' && '/admin/messange' ? null : <MyNav username={username} />

CodePudding user response:

You can use indexOf:

let HideHeader = window.location.pathname.indexOf('/admin/') === 0 ? null : <MyNav username={username} />

It will test for pathname that starts with /admin/

CodePudding user response:

Try to use includes() method:

let HideHeader = window.location.pathname.includes('/admin/') ? null : <MyNav username={username} />

CodePudding user response:

You can use match method with regex

let HideHeader =  !window.location.pathname.match(/\/admin.*/g) && <MyNav username={username} />

CodePudding user response:

You can use simple string methods like startsWith(), search(), indexOf(), includes()

https://www.w3schools.com/jsref/jsref_search.asp

https://www.w3schools.com/jsref/jsref_startswith.asp

https://www.w3schools.com/jsref/jsref_indexof.asp

startsWith

window.location.pathname.startsWith('/admin')

This will return true if your URL starts with /admin

search

(window.location.pathname.search('/admin') !== -1)

This will return true if your URL contains /admin

indexOf

(window.location.pathname.indexOf('/admin') !== -1)

This will return true if your URL contains /admin

CodePudding user response:

Most efficient, semantically correct and understandable solution - startsWith:

window.location.pathname.startsWith('/admin');
  • Related