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.
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;
URLSearchParams
is not supported in iOS.
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With