React - How to get parameter value from query string?

后端 未结 30 1697
温柔的废话
温柔的废话 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:59

    React Router 5.1+

    5.1 introduced various hooks like useLocation and useParams that could be of use here.

    Example:

    
    

    Then if we visited say

    http://localhost:3000/test/signin?_k=v9ifuf&__firebase_request_key=blablabla
    

    You could retrieve it like

    import { useLocation } from 'react-router';
    import queryString from 'query-string';
    
    const Dashboard: React.FC = React.memo((props) => {
        const location = useLocation();
    
        console.log(queryString.parse(location.search));
    
        // {__firebase_request_key: "blablabla", _k: "v9ifuf"}
    
        ...
    
        return 

    Example

    ; }

提交回复
热议问题