I am trying to redirect to same component but under diffract conditions and i am using windows.location.pathname as a way to know when the user is hitting the second endpoint my question is there a way to know the current location ? I tried to use context but it wont work because I don't want the context provider to give state to the main page that I am redirecting from also could my approach be considered bad ?
<Route path='/creator' exact component={maincomponent} />
<Route path='/viewer' exact component={maincomponent} />
If you want to learn the current path, you can use useLocation from react-router-dom.
For example:
import { useLocation } from 'react-router-dom'
function PathView() {
const location = useLocation();
const currentPath = location.pathname
return <span>{currentPath}</span>
}
But if you want to use Routes with if condition, you can simply use {}.
For example:
{"YOUR_VARIABLE" === "YOUR_VARIABLE" ? (
<Route path='/creator' exact component={maincomponent} />
) : (
<Route path='/viewer' exact component={maincomponent} />
)