Abhinav
Abhinav

Reputation: 44

How can i render PageNotFound.js if i am not provinding parameter value to URL?

When i am clicking on the link(having key containing auth token) which i am getting in my mail,it is redirecting me to Update password page.But I don't want to redirect it if a user is just entering the URL.

[http://localhost:3000/update-password?key=ZXlKaGJHY2lPaUpJVXpJMU5pSXNJblI1Y0NJNklrcFh] should redirect to Change Password page and [http://localhost:3000/update-password] should redirect to PageNotFound page.i hope you'll understand.

Upvotes: 0

Views: 38

Answers (1)

Oleg
Oleg

Reputation: 1168

You should follow three steps:

  1. Get current search from react-router:

    const {
       location: { search }
    } = propsFromReactRouter
    
  2. Check, does search contain key. You can do it with https://www.npmjs.com/package/query-string on mannualy.

    import { parse } from 'query-string';
    const searchData = parse(search);
    
    const { key } = searchData;
    
  3. if search doesn't contain key, redirect to not found page

    import {Redirect} from 'react-router-dom'
    // ...
    return (
        <Redirect to="/404" />
    )
    

Upvotes: 1

Related Questions