I am try to get a query string from a URL but am getting an error. I am using a reactjs project created with Shopify-CLI. I have tried the examples on
https://ui.dev/react-router-v4-query-strings/
https://ui.dev/react-router-v5-query-strings/
TypeError: Cannot read property ‘params’ of undefined
import React, { Component } from "react";
class Users extends Component {
render() {
const { id } = this.props.match.params;
return (
<div>
<h1>User id is {id}</h1>
</div>
);
}
}
export default Users;
2
Answers
You need to install query-string package
npm install query-string
For different approach you can look: React – How to get parameter value from query string?
Moreover, as your error shows
props
is undefined. If you are already parsing and getting the values in props from parent component then try logging the props value. Else you can directly use query-string.try this:
use it :
source: https://html-online.com/articles/get-url-parameters-javascript/