<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manoj Kumar Jain</title>
    <description>The latest articles on DEV Community by Manoj Kumar Jain (@manojkumarjain).</description>
    <link>https://dev.to/manojkumarjain</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1002954%2Fc1dd0229-a301-4f04-ac40-9fa6128c1d1a.png</url>
      <title>DEV Community: Manoj Kumar Jain</title>
      <link>https://dev.to/manojkumarjain</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manojkumarjain"/>
    <language>en</language>
    <item>
      <title>React context is lost with useNavigate</title>
      <dc:creator>Manoj Kumar Jain</dc:creator>
      <pubDate>Fri, 06 Jan 2023 12:03:27 +0000</pubDate>
      <link>https://dev.to/manojkumarjain/react-context-is-lost-with-usenavigate-3a6h</link>
      <guid>https://dev.to/manojkumarjain/react-context-is-lost-with-usenavigate-3a6h</guid>
      <description>&lt;p&gt;I am trying to navigate to  a link in my React application.&lt;br&gt;
I am  maintaining application state with React useContext hooks&lt;br&gt;
From my landing page, the context is initialized.&lt;br&gt;
On landing page I have a button to navigate to my profile page. For navigation I am  using react useNavigation hooks.&lt;br&gt;
The problem is with navigation, the   page opens correctly but the react context is lost and it shows blank data.&lt;br&gt;
Question -  how can I maintain the context while switching the pages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Routes, Route, Navigate } from "react-router-dom";

import { CZGlobalProvider } from './store/CZGlobalContext';
import CZHeader from './components/Header/CZHeader'
import Explore from "./components/CZWorld/Explore";
import MyProfile from "./components/MyWorld/MyProfile";
import EditMyProfile from "./components/MyWorld/EditMyProfile";

function CApp() {
  return (
    &amp;lt;div className="cmaze"&amp;gt;
      &amp;lt;CZGlobalProvider&amp;gt;
        &amp;lt;CZHeader /&amp;gt;
        &amp;lt;Routes&amp;gt;
          {&amp;lt;Route path="/" element={&amp;lt;Navigate replace to="explore" 
 /&amp;gt;} /&amp;gt;
          &amp;lt;Route path="explore" element={&amp;lt;Explore /&amp;gt;} /&amp;gt;
          &amp;lt;Route path="profile" element={&amp;lt;MyProfile /&amp;gt;} /&amp;gt;
          &amp;lt;Route path="editprofile" element={&amp;lt;EditMyProfile /&amp;gt;} /&amp;gt;
        &amp;lt;/Routes&amp;gt;
      &amp;lt;/CZGlobalProvider&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export {
  CApp
}




import { CZGlobalContext} from "../../store/CZGlobalContext";
import { Navigate, useNavigate } from "react-router-dom";
const HeaderMenu = () =&amp;gt; {
  const {
    appCtx, setAppCtx
  } = useContext(CZGlobalContext);

  const navigate = useNavigate();


  const handleOnClick = async (event) =&amp;gt; {
    event.preventDefault();
    console.log('handleOnClick ' + event.target.innerText)
    setAppCtx(
      {
        name: 'Manoj',
        email: 'manoj@testmail.com'
      }
    )
    navigate('/profile')
  }

  return (
    &amp;lt;React.Fragment&amp;gt; 
       &amp;lt;Button variant="contained" size="small" onClick={handleOnClick }&amp;gt;Profile&amp;lt;/Button&amp;gt;


    &amp;lt;/React.Fragment&amp;gt;
  );
}

export default HeaderMenu;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
