简体   繁体   English

ReactJS:防止href替换URL哈希值(令牌)

[英]ReactJS: Preventing href from replacing URL hash value(token)

Well this is awkward... as frankly enough, I have not been able to wrap my head around this supposedly simple solution. 好吧,这很尴尬……坦白地说,我还没能解决这个所谓的简单解决方案。 Basically, the web app generates an access token and a refresh token after login authentication from Spotify API and it works just fine per se and produces this: 基本上,该Web应用程序通过Spotify API进行登录身份验证后会生成访问令牌和刷新令牌,并且本身就可以正常工作并产生以下内容:

localhost:3000/#access_token=BQCs..&refresh_token=AQCc... 本地主机:3000 /#access_token = BQC ..&refresh_token = AQCc ...

However, I have a button ie 但是,我有一个按钮,即

 <Button href="#main1"> Foo? </Button>

that leads to a section of the page once it's pressed and that replaces the tokens in the URL so when one refreshes the page, the necessary tokens are not read and therefore, the endpoints will not function without it. 它会在按下页面时指向页面的一部分,并替换URL中的标记,因此当刷新页面时,将不会读取必需的标记,因此,没有该标记,端点将无法运行。

I have tried the following methods like: 我已经尝试过以下方法:

  • implementing event.preventDefault() that is triggered by an onClick() handler 实现由onClick()处理函数触发的event.preventDefault()
  • react-scrollable-anchor with goToAnchor('section1', false) 使用goToAnchor('section1', false) react-scrollable-anchor
  • a boolean value ie loggedIn: token ? true : false 一个布尔值,即loggedIn: token ? true : false loggedIn: token ? true : false where it conditionally renders the components provided there are tokens detected and window.location='localhost:3000/#access_token=' + access + '&refresh_token=' + refresh if there are none loggedIn: token ? true : false如果有条件检测到令牌,并且有条件地渲染组件,则window.location='localhost:3000/#access_token=' + access + '&refresh_token=' + refresh如果没有window.location='localhost:3000/#access_token=' + access + '&refresh_token=' + refreshwindow.location='localhost:3000/#access_token=' + access + '&refresh_token=' + refresh
  • react router with <Link to={params.access_token}> <Link to={params.access_token}> 对路由器做出反应

Unfortunately, the first two of these methods had the similar initial outcome due to its nature of completely removing the URL hash so it would be left with: 不幸的是,由于它们完全删除了URL哈希的性质,因此前两种方法的初始结果相似,因此将保留以下内容:

localhost:3000 本地主机:3000

The last two, on the other hand, just gives an undefined value of the tokens after refreshing the page which means the tokens aren't being stored even after its initial load. 另一方面,后两个仅在刷新页面后给出未定义的令牌值,这意味着即使在初始加载后也不会存储令牌。

What is the best approach in tackling this? 解决这个问题的最佳方法是什么? How does one keep the access tokens in the URL hash effectively? 如何有效地将访问令牌保留在URL哈希中? As always, any help and suggestions are highly appreciated. 与往常一样,任何帮助和建议都将受到高度赞赏。 Thanks. 谢谢。

Finally found an approach to go to a certain component without having to do any sort of hassle in anchoring the href and/or changing url hash: https://www.npmjs.com/package/react-scroll 最终找到了一种无需使用任何麻烦即可锚定href和/或更改url哈希值的方法: https : //www.npmjs.com/package/react-scroll

Please do note that this type of approach will only work on one-page web apps. 请注意,这种方法仅适用于一页Web应用程序。 It basically allows you to jump smoothly from one component to another. 基本上,它使您可以从一个组件平稳过渡到另一个组件。 I also looked into React-Spring but it didn't work out for me after fiddling for a whole day, on the other hand, react-scroll did the trick for me. 我也研究了React-Spring,但是在整整一天后,它对我来说还是没有用,另一方面,react-scroll为我解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM