Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find variable: URLSearchParams

Tags:

javascript

In React Native, I would like to do an abstraction of URLSearchParams so I write this class:

export class HttpSearchParamsAdapter extends URLSearchParams implements HttpSearchParams {

constructor() {
    super();
}

set(param: string, val: string): void {
    super.set(param, val);
    }
}

But when I build the app, I have this error: Can't find variable: URLSearchParams. I use URLSearchParams from es6.

like image 955
mickaelw Avatar asked Apr 08 '17 18:04

mickaelw


2 Answers

As mrgoos said, URLSearchParams is not supported in iOS which uses JavaScript Core, you can add a polyfill to React Native.

For example, I had a problem using wretch, so I used this polyfill globally: url-search-params-polyfill

// index.js
import 'url-search-params-polyfill';

UPDATE (for React Native 0.59)

In React Native 0.59, the developers "implemented" some methods of URLSearchParams with throw new Error('not implemented'); so... it miserably fails.

You can read the issue here: https://github.com/facebook/react-native/issues/23922

Instead of the polyfill I used, we can use whatwg-url implementation and the buffer package as explained here: https://github.com/facebook/react-native/issues/23922#issuecomment-476070032

// index.js
import { URL, URLSearchParams } from 'whatwg-url';
import { Buffer } from 'buffer';

// react-native 0.59 added its own global URLSearchParams without implementation...
// https://github.com/facebook/react-native/blob/e6057095adfdc77ccbbff1c97b1e86b06dae340b/Libraries/Blob/URL.js#L66
// Issue: https://github.com/facebook/react-native/issues/23922
global.Buffer = Buffer;
global.URL = URL;
global.URLSearchParams = URLSearchParams;
like image 173
Álvaro Larumbe Avatar answered Sep 30 '22 14:09

Álvaro Larumbe


URLSearchParams is not supported in iOS. https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

like image 36
mrgoos Avatar answered Sep 30 '22 13:09

mrgoos