creating a back button with react router
to create a back button using react router:
-
set the property on the button
onclick
to the function. -
use
usenavigate()
hooks, for exampleconst navigate = usenavigate();
. -
call
navigate()
the function, passing it-1 - navigate(-1)
.
import {link, routes, route, usenavigate} from 'react-router-dom';
export default functionapp() {
const navigate = usenavigate();
return (
<div>
<button onclick={() => navigate(-1)}>go back 1 pagebutton>
<button onclick={() => navigate(1)}>go forward 1 pagebutton>
<div>
<nav>
<ul>
<li>
<link to="/">homelink>
li>
<li>
<link to="/contacts">contactslink>
li>
ul>
nav>
{/* 👇️ wrap your route components in a routes component */}
<routes>
<route path="/contacts" element={<contacts />} />
<route path="/" element={<home />} />
routes>
div>
div>
);
}
function home() {
return <h2>homeh2>;
}
function contacts() {
return <h2>contactsh2>;
}
usenavigate
the hook returns a function that lets us navigate programmatically, such as after submitting a form or clicking a button.
to return to the previous page, pass -1 as an argument to
navigate()
the function, for examplenavigate(-1)
.
invoking navigation with -1 is the same as clicking the back button in your browser.
likewise, we can call the function with -2navigate
to go back 2 pages.
we can also use navigate
functions to programmatically navigate to different routes, navigate(/contacts)
e.g.
import {usenavigate} from 'react-router-dom';
export default function app() {
const navigate = usenavigate();
const handleclick = () => {
// 👇️ replace set to true
navigate('/contacts', {replace: true});
};
return (
<div>
<button onclick={handleclick}>navigate to /contactsbutton>
div>
);
}
when the replace property options
of a object is set to true , the current entry in the history stack will be replaced by the new entry.
in other words, navigating to a new route will not push a new entry into the history stack, so if the user clicks the back button, they will not be able to navigate to the previous page.
this is useful, for example, when a user is logged in - we don't want them to be able to click the back button and return to the login page.
or if we have a route that will use redirects to different pages - we don't want the user to click the back button and be redirected again.
if we need to navigate forward one page, call 1 - navigate(1)
navigation using .
to use the hook in your application usenavigate
, make sure the component in your index.jsapp
file is wrapped in a router.
import {createroot} from 'react-dom/client';
import app from './app';
import {browserrouter as router} from 'react-router-dom';
const rootelement = document.getelementbyid('root');
const root = createroot(rootelement);
// 👇️ wrap app in router
root.render(
<router><app />router>
);
the best place to wrap your react application with the router component is in the index.js file, since this is the entry point for your react application.
once your entire app router
is wrapped in a component, you can use any hook from the react router package anywhere in the component.
for reprinting, please send an email to 1244347461@qq.com for approval. after obtaining the author's consent, kindly include the source as a link.
article url:
related articles
how to create a read more/less button in react
publish date:2025/03/10 views:168 category:react
-
在构建 web 应用程序时,有时我们需要显示一些长文本。在这种情况下,一个聪明的设计是只显示部分文本并添加一个显示更多按钮供用户在需要时展开文本。当文本已展开并完全显示时
insert style tags into react components
publish date:2025/03/10 views:66 category:react
-
要在 react 组件中插入样式标签,请将我们的 css 包装在样式标签中的模板字符串中。 花括号之间的表达式将被评估并添加样式。
how to listen to state changes in react
publish date:2025/03/10 views:179 category:react
-
使用 useeffect 钩子来监听 react 中的状态变化。 我们可以将要跟踪的状态变量添加到挂钩的依赖项数组中,并且每次状态变量更改时都会运行 useeffect 钩子中的逻辑。
get parent height and width in react
publish date:2025/03/10 views:158 category:react
-
在 react 中获取父级的高度和宽度: 在元素上设置 ref 属性。 在 useeffect 钩子中,更新高度和宽度的状态变量。 使用 offsetheight 和 offsetwidth 属性来获取元素的高度和宽度。
get the value of an input field in react
publish date:2025/03/10 views:179 category:react
-
要在 react 中获取输入字段的值: 声明一个跟踪输入字段值的状态变量。 将 onchange 属性添加到输入字段。 使用 event.target.value 获取输入字段的值并更新状态变量。
rendering nested arrays in react using map()
publish date:2025/03/10 views:71 category:react
-
使用 map() 渲染嵌套数组: 使用 map() 方法迭代外部数组。 在每次迭代中,调用嵌套数组的 map()方法。 渲染嵌套数组的元素。
publish date:2025/03/10 views:118 category:react
-
在 react 中获取表单提交的输入值: 将输入字段的值存储在状态变量中。在表单元素上设置 onsubmit 属性。 在我们的 handlesubmit 函数中访问输入字段的值。
publish date:2025/03/10 views:130 category:react
-
在 react 中为 body 元素添加一个类: 在 useeffect 或事件处理程序中以 document.body 的形式访问 body 元素。 使用 classlist.add() 方法将类添加到 body 标记。 例如,document.body.classlist.add('my-class'
clearing the value of an input field in react.js
publish date:2025/03/10 views:119 category:react
-
要在 react 中清除输入字段的值:将输入的值存储在状态变量中。 当某个事件发生时,将状态变量设置为空字符串。对于不受控制的组件,将 ref 的值设置为空字符串,例如 ref.current.va