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
Maybe a bit late but this react hook can help you get/set values in URL query: https://github.com/rudyhuynh/use-url-search-params (written by me).
It works with or without react-router
.
Below is code sample in your case:
import React from "react";
import { useUrlSearchParams } from "use-url-search-params";
const MyComponent = () => {
const [params, setParams] = useUrlSearchParams()
return (
<div>
__firebase_request_key: {params.__firebase_request_key}
</div>
)
}
this.props.params.your_param_name
will work.
This is the way to get the params from your query string.
Please do console.log(this.props);
to explore all the possibilities.
In the component where you need to access the parameters you can use
this.props.location.state.from.search
which will reveal the whole query string (everything after the ?
sign)
With this one-liner, you can use it anywhere in both React Hook and React Class Component with plain JavaScript.
https://www.hunterisgod.com/?city=Leipzig
let city = (new URLSearchParams(window.location.search)).get("city")
React Router v4 and React Router v5, generic
React Router v4 does not parse the query for you any more, but you can only access it via this.props.location.search
. For reasons see nbeuchat's answer.
E.g. with qs library imported as qs
you could do
qs.parse(this.props.location.search, { ignoreQueryPrefix: true }).__firebase_request_key
Another library would be query-string. See this answer for some more ideas on parsing the search string. If you do not need IE-compatibility you can also use
new URLSearchParams(this.props.location.search).get("__firebase_request_key")
For functional components you would replace this.props.location
with the hook useLocation. Note, you could use window.location.search
, but this won't allow to trigger React rendering on changes.
If your (non-functional) component is not a direct child of a Switch
you need to use withRouter to access any of the router provided props.
React Router v3
React Router already parses the location for you and passes it to your RouteComponent as props. You can access the query (after ? in the url) part via
this.props.location.query.__firebase_request_key
If you are looking for the path parameter values, separated with a colon (:) inside the router, these are accessible via
this.props.match.params.redirectParam
This applies to late React Router v3 versions (not sure which). Older router versions were reported to use this.props.params.redirectParam
.
General
nizam.sp's suggestion to do
console.log(this.props)
will be helpful in any case.
Or perhaps something like this?
let win = {
'location': {
'path': 'http://localhost:8000/#/signin?_k=v9ifuf&__firebase_request_key=blablabla'
}
}
if (win.location.path.match('__firebase_request_key').length) {
let key = win.location.path.split('__firebase_request_key=')[1]
console.log(key)
}