← Back to React Course | Chapter 8: Routing | Lesson 7 of 10

Programmatic तरीके से Navigation

Programmatic navigation आपके GPS को voice command से कहीं जाने के लिए बताने जैसा है, map पर physically एक link click करने के बजाय।
Syntax
markup
const navigate = useNavigate();

navigate("/path");
navigate("/path", { replace: true });
navigate(-1); // go back

एक Click की जगह एक Action के बाद Navigate करना

कभी-कभी navigation को code run होने के result के रूप में होना चाहिए, जैसे एक successful form submission के बाद redirect करना, user द्वारा directly एक Link click करने के बजाय।

useNavigate() hook एक function return करता है जिसे आप किसी भी event handler या effect के अंदर से call करके वह navigation trigger कर सकते हैं।

Note: navigate() को अपने async submit logic succeed होने के right बाद call करें, ताकि redirect सिर्फ तब हो जब action असल में काम किया हो।
Warning: useNavigate() को एक BrowserRouter के अंदर rendered एक component के अंदर call किया जाना चाहिए — इसे उस context के बाहर call करना एक error throw करता है।

उदाहरण: Navigating After an Action, Not a Click

markup
// Run in your local React project (npm install required)
import { useNavigate } from 'react-router-dom';

function LoginForm() {
  const navigate = useNavigate();
  const handleSubmit = e => {
    e.preventDefault();
    navigate('/dashboard');
  };
  return <form onSubmit={handleSubmit}><button type="submit">Log In</button></form>;
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

navigate(-1) के साथ Back जाना

navigate को एक negative number pass करना browser के back button की तरह act करता है, history में इतने steps पीछे move करते हुए। navigate(-1) एक page पीछे जाता है, एक Cancel या Back button के natural behavior को mimic करते हुए।

Note: यह एक generic Back button implement करने का एक nice तरीका है जो काम करता है भले ही user कहीं से भी आया हो।
Warning: अगर history में कोई previous entry न हो (जैसे user directly इस page पर land किया हो), navigate(-1) आपके app से पूरी तरह बाहर navigate कर सकता है।

उदाहरण: Going Back with navigate(-1)

markup
// Run in your local React project (npm install required)
import { useNavigate } from 'react-router-dom';

function BackButton() {
  const navigate = useNavigate();
  return <button onClick={() => navigate(-1)}>Go Back</button>;
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Push करने के बजाय History Replace करना

Default रूप से, navigate('/path') browser history में एक नई entry जोड़ता है, इसलिए back button previous page पर return करता है। दूसरे argument के रूप में { replace: true } pass करना इसके बजाय current entry swap कर देता है, जो एक login redirect के बाद useful है ताकि back button login form पर return न हो।

Note: उन redirects के लिए replace:true उपयोग करें जिन्हें user back button से undo नहीं कर पाना चाहिए, जैसे post-login या post-logout redirects।
Warning: कहीं और replace:true को overuse करना users को confuse कर सकता है इनके expected back-button behavior को तोड़ते हुए।

उदाहरण: Replacing History Instead of Pushing

markup
// Run in your local React project (npm install required)
import { useNavigate } from 'react-router-dom';

function LoginForm() {
  const navigate = useNavigate();
  const handleSubmit = e => {
    e.preventDefault();
    navigate('/dashboard', { replace: true });
  };
  return <form onSubmit={handleSubmit}><button type="submit">Log In</button></form>;
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. पुराने useHistory hook का उपयोग करने की कोशिश करना, जो React Router v6 में useNavigate के favor में हटाया गया।
  2. useNavigate() को एक component के बाहर call करना जो एक BrowserRouter के अंदर rendered नहीं है।
  3. यह भूल जाना कि navigate('/path') default रूप से browser history में जोड़ता है — अगर आप इसके बजाय replace करना चाहें तो { replace: true } उपयोग करें।
चैप्टर सारांश
  • useNavigate() एक function return करता है जिसे आप event handlers या effects के अंदर से routes बदलने के लिए call करते हैं।
  • navigate('/path') browser history पर एक नई entry push करता है, एक Link click करने जैसा ही।
  • navigate(-1) history में एक step पीछे जाता है, browser के back button जैसा।
  • दूसरे argument के रूप में { replace: true } current history entry replace करता है एक नई जोड़ने के बजाय।
ब्राउज़र सपोर्ट

npm install react-router-dom (v6 useNavigate) चाहिए — इस sandbox में CDN से available नहीं; v5 इसके बजाय useHistory उपयोग करता था।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.