简体   繁体   English

React js-单击链接后如何防止页面重新加载。 现在整个页面都在点击链接时刷新

[英]React js-How to prevent page reload once click on the link. Right now the whole page is getting refresh on clicking the link

I have link in which is the invoice number so once i click on it,it should provide data of particular invoice number without reloading the page.[code]我有发票号码的链接,所以一旦我点击它,它应该提供特定发票号码的数据而无需重新加载页面。[代码]

Routing is normally handled by the browser , which sends HTTP requests when the URL changes.路由通常由浏览器处理,当 URL 更改时,浏览器会发送 HTTP 请求。
Here it seems you want your app to handle URL changes, so that it displays content depending on the URL.在这里,您似乎希望您的应用程序处理 URL 更改,以便它根据 URL 显示内容。 This is called client-side routing, you can do it using:这称为客户端路由,您可以使用:

Easy way to redirect another page without refreshing the page...在不刷新页面的情况下重定向另一个页面的简单方法...

import React from 'react';
import { Link } from "react-router-dom";

addpost is route name addpost 是路由名称

<ul>
<li><Link to="/addpost">AddPost</Link> </li>
</ul>

use components that are provided by the 'react-router-dom' package, instead of the anchor tag 'a', use 'Link' or 'NavLink' components example : from this => <a href='#'>my-link></a> to this => <Link to='#'>my-link></Link>使用'react-router-dom' 包提供的组件,而不是锚标签'a',使用'Link' 或'NavLink' 组件示例:从这个=> <a href='#'>my-link></a>到此 => <Link to='#'>my-link></Link>

for more information , checkout the official documentation https://reactrouter.com/docs/en/v6/examples/basic有关更多信息,请查看官方文档https://reactrouter.com/docs/en/v6/examples/basic

You could prevent the page from reloading by using preventDefault method in the event practice.您可以通过在事件实践中使用preventDefault方法来阻止页面重新加载。

function onLinkClick(e) {
   e.preventDefault();
   // further processing happens here
}

<a href="/my-invoice-link" onClick={onLinkClick} />

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

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