简体   繁体   English

React-router中Link和Anchor标签之间的不同行为

[英]The different behavior between Link and Anchor tag in React-router

I think it is a convention to use Link over Anchor tag. 我认为使用Link over Anchor标签是一种惯例。 From my understanding, Link tag will be translated into anchor tag into html. 根据我的理解,链接标记将转换为html的锚标记。 However, from my observation, Link will render the page in the client side and also cache the page, the anchor tag will render the page in the server side. 但是,根据我的观察, Link会将页面呈现在客户端,并且还将缓存该页面, anchor标签会将页面呈现在服务器端。

Here is the code, I have one component which has method willTransitionTo 这是代码,我有一个组件,其中有一个方法willTransitionTo

willTransitionTo: function (transition, params, query, callback) {
      console.log('transition to');
    }

When I have another component to link this component, if I am using Link from react-router, the log will be printed in the browser console. 当我有另一个组件可以链接该组件时,如果我正在使用React-router的Link,则日志将打印在浏览器控制台中。 The interesting thing is that it cached the data on the second time when I link to this page (no data fetched from the network). 有趣的是,当我链接到该页面时,它第二次缓存了数据(没有从网络中获取数据)。 On the other hand, the log will be printed in the server side console if I am using anchor. 另一方面,如果使用锚点,则日志将打印在服务器端控制台中。

I understand what anchor does, but I dont understand why anchor in the Link is client side rendering. 我了解锚点的作用,但我不理解为什么链接中的锚点是客户端渲染。 Is it an expected behavior ? 这是预期的行为吗? Also, how Link know how to cache the data if I dont misunderstand. 另外,如果我没有误会,Link如何知道如何缓存数据。

Many thanks in advance 提前谢谢了

I'm unsure exactly what you're asking. 我不确定你到底在问什么。 I think what you're asking is why the URL in the browser changes without it generating a new HTTP request. 我认为您要问的是为什么浏览器中的URL更改而没有生成新的HTTP请求。 The reason is HTML5 history. 原因是HTML5历史记录。

HTML5 history allows us to have nice URLs in front-end routing instead of the old style hash based URLs, ie /page instead of /#page . HTML5历史记录使我们可以在前端路由中使用漂亮的URL,而不是基于旧式哈希的URL,即/page代替/#page In this sense, React Router is nothing more than a wrapper around HTML5 history. 从这个意义上讲,React Router只不过是HTML5历史的包装。

When using HTML5 history, it's important to make sure the back end is configured to serve up the pages at all your routes. 使用HTML5历史记录时,请务必将后端配置为在您的所有路线上提供页面。 eg You can have a front-end transition from / to /cart , but if you then reload the page you'll get a server error if your server hasn't been configured to serve the endpoint /cart . 例如,您可以从//cart进行前端转换,但是如果您没有重新配置页面以服务于端点/cart ,则如果重新加载页面,则会收到服务器错误。 This is also important for older browsers (eg IE9) that don't support HTML5 history, which will gracefully degrade to traditional HTTP requests. 这对于不支持HTML5历史记录的较旧的浏览器(例如IE9)也很重要,该浏览器可以正常降级为传统的HTTP请求。

For more on HTML5 history, there's the always readable CSS Tricks. 有关HTML5历史记录的更多信息,请参阅始终可读的CSS技巧。 https://css-tricks.com/using-the-html5-history-api/ https://css-tricks.com/using-the-html5-history-api/

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

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