1

My application uses angularJS, so I'm migrating it to React. I'm navigating to a route: /index like this $urlServiceProvider.rules.otherwise('/index').Note that I'm using angularJS's urlServiceProvider to navigate to /index which has $stateProvider set like this :

$stateProvider.state('index', {
     component: AppController,
     url: '/index',
     template: `<div id="application"></div>`
})

// AppController.jsx

import * as ReactDOM from 'react-dom'
import * as React from 'react'
import App from './App'
export class AppController implements ng.IComponentController {
  constructor() {}

  $onInit(): void {
    ReactDOM.render(<App />, document.getElementById('application'))
  }
}

<App /> component is a component in react which has routes defined for every page.

Now, this works fine but what If I want to set queryString params, which I did it like this: /index?redirect=true. Now, inside <App /> component if I log the value like:

// App

const { redirect } = useLocation()
console.log('Redirect value is ', redirect) // Expecting true here

I get nothing printed in console. Am I doing it correct here?

2
  • What is useLocation hook here? Is it the useLocation hook from react-router-dom? What version of react-router-dom do you have installed? Commented May 10, 2022 at 23:58
  • Yes, its useLocation hook from react-router-dom v6 Commented May 10, 2022 at 23:59

1 Answer 1

3

If using react-router-dom@6 you can use the useSearchParams hook.

Example:

import { useSearchParams } from 'react-router-dom';

...

const [searchParams] = useSearchParams();
const redirect = searchParams.get("redirect");

If using react-router-dom@5 you will need to use the useLocation hook and access the location.search value and instantiate your own URLSearchParams object.

Example:

import { useLocation } from 'react-router-dom';

...

const { search } = useLocation();
const searchParams = new URLSearchParams(search);
const redirect = searchParams.get("redirect");

In RRDv5 you can abstract a custom hook to do this for you:

const useQuery = () => {
  const { search } = useLocation();
  return React.useMemo(() => new URLSearchParams(search), [search]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I got the console to print but it's throwing null as the value. This is what I'm passing into urlServiceProvider : $urlServiceProvider.rules.otherwise('/index?redirect=true') and in $stateProvider : $stateProvider.state('index', { component: AppController, url: '/index', template: `<div id="application"></div>` }). I don't think I've to do changes in $stateProvider here right?
@SimmiGeorge I wouldn't think you would, but my Angular knowledge is pretty minimal and limited really old Angular 1 or 2 stuff, and even that I only learned the bare minimum to get stuff to work (React app built on top of an older Angular app base 😠).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.