convert routeParams from string to number

Viewed 694

I have a simple project and in this project I am trying to return data through the id number, and I used “routeParams”, but I had a problem that the “routeParams” is of a string type and I want to convert it to a number, how can I do that?

 const routeParams = useParams();
 
  useDeepCompareEffect(() => {
    dispatch(getReceipt(routeParams.orderId));
  }, [dispatch, routeParams.orderId]);
3 Answers

You can convert it by using Number().

const routeParams = useParams();

 useDeepCompareEffect(() => {
   dispatch(getReceipt(Number(routeParams.orderId)));
 }, [dispatch, routeParams.orderId]);

You can use parseInt it will return parsed number if you have correct value or NaN if not

You can use the plus sign before the string to make it a number

const routeParams = useParams();
 
  useDeepCompareEffect(() => {
    dispatch(getReceipt(+routeParams.orderId));
  }, [dispatch, routeParams.orderId]);
Related