Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to use single route for two dashboard in React

Here, what I’m trying to do is to make React App with 2 dashboards. 1st, if kyc_status is true then react app should display <Home2> and 2nd, if kyc_status is false then <Dashboard> after login.

Using refresh_token api. I’m storing value of kyc_status in a state(kyc) and using conditional rendering, I’m checking which route react app should take.

But It’s landing on <Dashboard> first, then re-route itself on <Home2> in case if kyc_status is true. I don’t know it’s prioritizing <Dashboard>.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Code Image

const DashboardLayout = () => {
  let navigate = useNavigate();
  const [kyc, setKyc] = useState();

  async function getUser() {
    getDataAPI("user/refresh_token").then(function (token) {
      if (token.data.accesstoken) {
        getDataAPI(`get_user/${token.data.user._id}`).then((res) =>
          setKyc(res.data.kyc_status)
        );
      }
    });
  }
  useEffect(() => {
    getDataAPI("user/refresh_token").then(function (res) {
      if (res.data.status == 0) {
        console.log(res.data.status);
        navigate("/login");
      }
    });

    getUser();
  }, []);
  return (
    <Fragment>
      <Header />
      <Routes>
        {kyc ? (
          <Route exact path="/" element={<Home2 />}></Route>
        ) : (
          <Route exact path="/" element={<Dashboard />}></Route>
        )}
      </Routes>
    </Fragment>
  );
};

export default DashboardLayout;

>Solution :

One thing you can do is to have kyc as undefined first, and then when it loads, you would show whichever dashboard you would want to show.

Something like this:

const [kyc, setKyc] = useState(undefined);
...

{kyc === undefined ? (
    //Display some loading screen here
) : (
    <Route exact path="/" element={kyc ? <Home2 /> : <Dashboard />} />
)}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading