Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URLSearchParams is not a constructor in ReactJS

I am facing this error while using URLSearchParams

TypeError: url__WEBPACK_IMPORTED_MODULE_2__.URLSearchParams is not a constructor

This is how I am calling:

componentDidMount() {
    const query = new URLSearchParams(this.props.location.search); // <- ERORR
    const ingredients = {};
    for (let param in query.entries()) {
        // ['salad', '1']
        ingredients[param[0]] = +param[1];
    }
    this.setState({ingredients : ingredients});
}

My ReactJS Project details:

"dependencies": {
"axios": "^0.19.0",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1"
},
like image 846
Faizan Mubasher Avatar asked Aug 25 '19 06:08

Faizan Mubasher


1 Answers

This happens when react import UrlSearchParams from 'url' So you have to remove this and try it. It will definitely work.

like image 134
Sandeep chand Avatar answered Oct 21 '22 22:10

Sandeep chand