如何在我的路由中定义路由。jsx文件捕获__firebase_request_key参数值从一个URL生成的Twitter的单点登录过程后,从他们的服务器重定向?
http://localhost:8000/#/signin?_k=v9ifuf&__firebase_request_key=blablabla
我尝试了以下路由配置,但:redirectParam没有捕获提到的参数:
<Router>
<Route path="/" component={Main}>
<Route path="signin" component={SignIn}>
<Route path=":redirectParam" component={TwitterSsoButton} />
</Route>
</Route>
</Router>
从v4开始,React路由器不再直接在其location对象中提供查询参数。原因是
There are a number of popular packages that do query string
parsing/stringifying slightly differently, and each of these
differences might be the "correct" way for some users and "incorrect"
for others. If React Router picked the "right" one, it would only be
right for some people. Then, it would need to add a way for other
users to substitute in their preferred query parsing package. There is
no internal use of the search string by React Router that requires it
to parse the key-value pairs, so it doesn't have a need to pick which
one of these should be "right".
包含了这个之后,只解析location会更有意义。在需要查询对象的视图组件中搜索。
你可以通过覆盖react-router中的withRouter来实现这一点
customWithRouter.js
import { compose, withPropsOnChange } from 'recompose';
import { withRouter } from 'react-router';
import queryString from 'query-string';
const propsWithQuery = withPropsOnChange(
['location', 'match'],
({ location, match }) => {
return {
location: {
...location,
query: queryString.parse(location.search)
},
match
};
}
);
export default compose(withRouter, propsWithQuery)
我使用了一个名为query-string的外部包来解析url参数,如下所示。
import React, {Component} from 'react'
import { parse } from 'query-string';
resetPass() {
const {password} = this.state;
this.setState({fetching: true, error: undefined});
const query = parse(location.search);
return fetch(settings.urls.update_password, {
method: 'POST',
headers: {'Content-Type': 'application/json', 'Authorization': query.token},
mode: 'cors',
body: JSON.stringify({password})
})
.then(response=>response.json())
.then(json=>{
if (json.error)
throw Error(json.error.message || 'Unknown fetch error');
this.setState({fetching: false, error: undefined, changePassword: true});
})
.catch(error=>this.setState({fetching: false, error: error.message}));
}