2

I am having URL as http://example.com/callback?code=abcd

I need to fetch value of code. Below is my code:

import React from 'react';
import { useEffect } from 'react';

const Callback = () => {
useEffect(() => {
    console.log("hiii");
    
}, []);

return (
    <React.Fragment>
        Callback
    </React.Fragment>
);
};

export default Callback;

Please confirm how can I fetch that. Thanks in advance.

1
  • your link not found Commented Oct 1, 2020 at 10:07

5 Answers 5

3

Probably the best way is to use the useParams hook because you have a functional component. Read from the documentation as the following:

useParams returns an object of key/value pairs of URL parameters. Use it to access match.params of the current <Route>.

I would suggest the following:

import React from 'react'
import { useParams } from 'react-router-dom'

const Callback = () => {
   let { code } = useParams()

   console.log({ code });

   return (
      <React.Fragment>
          Callback
      </React.Fragment>
   )
};

+1 from documentation:

You need to be using React >= 16.8 in order to use any of these hooks!

Sign up to request clarification or add additional context in comments.

Comments

1

use this library 'query-string' and use as

import queryString from 'query-string';

in constructor

  constructor(props) {
    const { location: { search } } = props;
    const parsed = queryString.parse(search);
     this.state = {
      code: parsed.code,
       }
   }

hope it will solve your problem !

Comments

1

You need to use a hook...

useEffect(() => {
  let urlParams = new URLSearchParams(window.location.search);
  console.log(urlParams.code || "empty");
}, []);

Comments

0

You can use the URLSearchParams interface to fetch the query string of a URL. https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

const queryParams = new URLSearchParams(window.location.search);
const code = queryParams.get("code");

1 Comment

You're forgetting about rendering and useEffect, the question is about React.
0

First at App.js you should have react-router-dom

import { Route, Switch } from "react-router-dom";

Then inside switch you can define route parameters like in your case

<Route exact path="/callback/:code" component={YourComponentsName} />

At the components file you can get the parameters from the props

Functional Component

const code = props.match.params.code

Class Component

 const code = this.props.match.params.code

Or next approach

const code = new URLSearchParams(this.props.location.search);    

const code = query.get('code')

console.log(code)//abcd

1 Comment

This will not work with the search query param. You need to parse window.location.search and that cannot be done with react-router paths

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.