How can I check whether there is any query string with route v6? I would like to achieve the same result as below ternary condition with searchParams
const redirect = location.search ? location.search.split("=")[1] : "/";
useEffect(() => {
if (userInfo) {
navigate(redirect);
}
}, [navigate, userInfo, redirect]);
For instance, if there is any query string like the below it should redirect to shipping if not it should redirect to /
const checkoutHandler = () => {
navigate('/login?redirect=shipping')
}
I tried something below but it did not work for me.
const [searchParams] = useSearchParams();
const redirect = searchParams ? searchParams.get('redirect') : "/";
App.js
<BrowserRouter>
<Header />
<Container>
<Routes>
<Route path="/shipping" element={<ShippingScreen />} />
<Route path="/login" element={<LoginScreen />} />
<Route path="/register" element={<RegisterScreen />} />
<Route path="/" element={<HomeScreen />} />
</Routes>
</Container>
<Container>
<Footer />
</Container>
</BrowserRouter>