1️⃣ Why URLs give strings?
-
Everything in a URL is text only.
-
When React Router reads a URL, it gives values like
"12","100"as strings, not numbers. -
Example:
URL:
http://localhost:3000/user/15
15→"15"(string)
2️⃣ What is useParams()?
-
useParams()is a React Router hook. -
It returns all dynamic values from the URL.
-
Example:
-
If your route is:
And your URL is
/user/10,
then:
3️⃣ Why do we use parseInt() with useParams()?
Since URL params come as strings, but sometimes we need numbers, we convert them:
⭐ Final Combined Example
🔥 Quick Summary
-
URLs only store strings, never numbers.
-
useParams()gets the values from the URL but always as strings. -
parseInt()is used to convert URL values (strings) into numbers when needed.
If you want, I can also explain parseFloat(), Number(), or the difference between them!
Comments
Post a Comment