Reactjs 将传递的url参数重定向到状态
使用“react router dom”:“^5.1.2” 我正在导航到一个url: http://localhost:3000/#id_token=123&expires_in=3600&token_type=Bearer 我有一个重定向组件,可以捕捉到这一点并将我重定向到仪表板:Reactjs 将传递的url参数重定向到状态,reactjs,react-router,Reactjs,React Router,使用“react router dom”:“^5.1.2” 我正在导航到一个url: http://localhost:3000/#id_token=123&expires_in=3600&token_type=Bearer 我有一个重定向组件,可以捕捉到这一点并将我重定向到仪表板: <Redirect exact from="/" to="/dashboard" /> 我知道我们可以重定向并将状态传递给道具(从react router文
<Redirect exact from="/" to="/dashboard" />
我知道我们可以重定向并将状态传递给道具(从react router文档),如下所示:
<Redirect
to={{
pathname: "/dashboard",
search: "?utm=your+face",
state: { referrer: currentLocation }
}}
/>
但是,我不知道如何将参数从url传递到状态(id_令牌、expires_in等)
这样做的解决方案是什么?您可以在重定向之前提取URL参数,并在重定向路由状态下传递它们
window.location.hash
将在url-中为您提供信息。您可以创建一个util函数来解析散列并将键和值返回给您。然后您可以在重定向中传递它
<Redirect
to={{
pathname: "/dashboard",
search: "?utm=your+face",
state: { id_token, expires_in, token_type }
}}
/>
谢谢!window.location.hash正是我所需要的。