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

How to use either react router useParams() or window.localStorage()

In my scenario , the component should take a value either in the URL or a locale storage but when I attempt to get value from useParams() it throw an error:

Uncaught ReferenceError: useParams is not defined

Really how can I handle this error ?

My code is:

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

const { userId } = useParams() || props;

How can I fix it?

>Solution :

I think you didn’t import useParams, in which case do so at the top of your file :

import {useParams} from "react-router-dom"

Then do :

let {userId} = useParmas();
if(!userId) userId = props.userId;

Because useParams return an object, an empty object like so {} if there isn’t any parameter, but {} is not false, so by doing so useParams() || props, if there isn’t an userId parameter in you your url, you will always get undefined.

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