React - How to get parameter value from query string?

后端 未结 30 1700
温柔的废话
温柔的废话 2020-11-22 10:22

How can I define a route in my routes.jsx file to capture the __firebase_request_key parameter value from a URL generated by Twitter\'s single sign on process a

30条回答
  •  感情败类
    2020-11-22 10:56

    As far as I know there are three methods you can do that.

    1.use regular expression to get query string.

    2.you can use the browser api. image the current url is like this:

    http://www.google.com.au?token=123
    

    we just want to get 123;

    First

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

    Then

    const token = query.get('token')
    console.log(token)//123
    

    3. use a third library called 'query-string'. First install it

    npm i query-string
    

    Then import it to the current javascript file:

     import queryString from 'query-string'
    

    Next step is to get 'token' in the current url, do the following:

    const value=queryString.parse(this.props.location.search);
    const token=value.token;
    console.log('token',token)//123
    

    Hope it helps.

    Updated on 25/02/2019

    1. if the current url looks like the following:

    http://www.google.com.au?app=home&act=article&aid=160990

    we define a function to get the parameters:

    function getQueryVariable(variable)
    {
            var query = window.location.search.substring(1);
            console.log(query)//"app=article&act=news_content&aid=160990"
            var vars = query.split("&");
            console.log(vars) //[ 'app=article', 'act=news_content', 'aid=160990' ]
            for (var i=0;i

    We can get 'aid' by :

    getQueryVariable('aid') //160990
    

提交回复
热议问题