facebook / react-native

A framework for building native applications using React

Home Page:https://reactnative.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do I use superagent from react-native?

gaearon opened this issue · comments

commented

Superagent fails trying to require() what I presume to be Node modules, such as tty.

Is React Native supposed to be browserish or nodeish environment from third-party package point of view? Should React Native expose Node modules like events?

We're still unclear about this. We're trying to be very deliberate in the dependencies that we add inside of core.

You can use fetch, the new w3c api in order to do data fetching. We also have xhr implemented.

For node modules like events, this is unclear. I wish that we could push events to npm instead of having it inside of react native directly.

Would love to get your point of view on this subject

I had a little more luck by using the browser build of superagent but it seems there's more of xhr that hasn't been ported yet that trips superagent up. Personally I'd love to see superagent supported as I already use it on the client and server and would be great if all that code could be shared. I'll see if I can work on Libraries/XMLHttpRequest/XMLHttpRequest.ios.js to get superagent working.

commented

Isn't module system supposed to take browser “redirects” into account as specified in package.json? I think it's the reason I didn't have this problem with Webpack:

(from superagent's package.json:)

  "browser": {
    "./lib/node/index.js": "./lib/client.js",
    "emitter": "component-emitter",
    "reduce": "reduce-component"
  },
  "main": "./lib/node/index.js",

React Native's bundler doesn't seem to look at these.

Which triggers another question: why does RN use its own bundler instead of Webpack or Browserify? There may be other potential issues lurking in there, already solved by existing bundlers. There is an entire ecosystem of transformers, including support for AMD or ES6 or transpile-to-JS languages.

Shouldn't we leverage this by default?

I answered this in another thread #5 (comment)

This is still a problem, isn't it? Would RN packager plan to fix this? Thanks 👍

Hi!
You can try 'superagent/lib/client' but you need to modify require statements at the top of file:
var Emitter = require('component-emitter');
var reduce = require('reduce-component');
Because React Native doesn't correctly handle 'browser' section in package.json.

Also you can try my fork that is work with React Native:
https://github.com/philipshurpik/superagent
It has some other little fixes... Mainly default content type fix - set "application/json" as default, because react native xhr request doesn't correctly handle it.

The packager now looks at the browser field. superagent should be usable once they publish a new version following this PR ladjs/superagent#621 which just makes it not depend on a location object on the global object.