Make Relay work with React Native out of the box
The remaining steps are:
- [x] Relay/React Native/fbjs versioning
- [x] Use the appropriate
unstableBatchedUpdatesdepending on React/React Native - [x] Version of
fetchpolyfill for React Native - [ ] Document the use of
babel-relay-pluginwith React Native (see discussion at https://github.com/facebook/relay/pull/714#issuecomment-181204627) - [x] Create a fresh React Native project, set up Relay, configure the plugin per the documentation added in the previous step, and make sure everything works as expected.
Note: edited by @josephsavona
have not tried yet, but looks like so. some examples from docs use react native components https://facebook.github.io/relay/docs/guides-containers.html#content
Relay should work as-is on React Native - if not, feel free to let us know and re-open this!
@josephsavona even if relay components aren't inheriting react-native's Component?
https://github.com/facebook/relay/blob/master/src/container/RelayContainer.js#L23
I'm getting Can't find variable: document in getActiveElement
I also needed to include johanneslumpe/react-native-browser-polyfill to get around a similar issue Can't find variable: self in whatwg-fetch
@josephsavona Relay cannot work as-is with React Native because Relay includes React web as a hard dependency, and including React web into React Native is not functional.
Here's a commit that shows the minimal changes to build relay against react-native: https://github.com/facebook/relay/commit/e0037c78931edc3286e5217894348ca8ece1225b
The 'dist' step of building relay still fails with that commit, with the error:

I'm not sure exactly why that's failing.
The last two things we had to do to get relay working with react native was:
- Commit relay's
/libdirectory since buildingdistfailed (above) - stub
process.envbefore including Relay in the react-native application, sinceprocessdoesn't exist in react-native's JS environment. This is only necessary until thediststep of compilation works, since dist will remove allprocess.envreferences. I made a pseudo-module called 'relay',, which is a bit silly but functional:
process = {env: {}};
module.exports = require('react-relay');
Last thing I forgot to mention regarding relay's compatibility with react-native:
relay assumes self is present. This is true in react-native only when debugging in Chrome. If you are not debugging in Chrome you will get an error self is not defined when including Relay. A solution is to add https://github.com/johanneslumpe/react-native-browser-polyfill to your react-native project.
@josephsavona can we re-open this?
@amccloud thanks for investigating, I'm reopening to give us a place to track this.
The last thing we needed to do to get relay working with react-native was to use react-native's global fetch by deleting the var fetch = require('fbjs/lib/fetch'); line from lib/RelayDefaultNetworkLayer.js.
We've gotten basic requests working following the steps I've outlined above in my comments on this issue, but I'm not 100% certain other things aren't broken yet.
@boourns that's great. I also had to delete var fetch = require('fbjs/lib/fetch') from the following library which was declared in lib/RelayDefaultNetworkLayer.js:
var fetchWithRetries = require('fbjs/lib/fetchWithRetries');
@boourns @clintonwoo Hello guys, followed your step all the way, however i get an error 'can't find variable fetchWithRetries', however when adding it i get 'cannot find variable: self'. Any tips would be greatly appreciated. github repo is https://github.com/almasakchabayev/reactNativeRelayWebpackDemo.
@almasakchabayev follow this comment: https://github.com/facebook/relay/issues/26#issuecomment-134767414
@boourns It worked when I changed fetchWithRetries in 'function _sendQuery' to just fetch. Great! Thanks a lot!
@boourns Maybe a dumb question: What do you mean by: "Commit relay's /lib directory since building dist failed (above)" ?
@lukasreichart
Ideally we would build relay against react-native, do a dist build which would result in a single relay.js file, which could be copied into the react-native project. The minimal changes to build relay against react-native are here: https://github.com/facebook/relay/commit/e0037c78931edc3286e5217894348ca8ece1225b
With that commit applied, the src to lib transpilation step succeeds, but doing a dist build of relay fails. See the screenshot posted in my original comment. So that commit is not sufficient in the long-term.
The proper solution will be to figure out why the dist build fails and making it not fail, but I don't really know what the problem is there yet.
In the meantime if you want to try relay with react native, the hack solution is to fork relay, apply https://github.com/facebook/relay/commit/e0037c78931edc3286e5217894348ca8ece1225b, commit /lib and then add that relay fork to your react-native project as an npm dependency. And then make the other changes I outline.
@boourns Thanks you've directed me to the right path. But now I have a problem with the babel-relay-plugin. I'am using this script. But keep getting an Error: Invariant Violation: RelayQL: Unexpected invocation at runtime. Either the Babel transform was not set up, or it failed to identify this call site. Make sure it is being used varbatim as Relay.QL``
This error occurs when you use Relay.QL ( which should have been replaced by the babel-relay-plugin). The strange thing is, that in index.ios.bundle, all Relay.QLstatements have been replaced correctly.
Any ideas on this one?
@lukasreichart in package.json for your project change the start command to be:
"start": "node_modules/react-native/packager/packager.sh --transformer ./lib/babelTransform.js"
And then you need to run your react-native packager via 'npm start' instead of running react-native start or using the packager that xcode boots up.
This is another workaround we had to apply, not sure how to make this just gracefully happen yet.
@boourns Would you mind sharing the content of your ./lib/babelTransform.js file?
Sorry, discussing with @jahfer now who did this portion of the work. Disclaimer again that this is hacks on hacks on hacks, not a robust solution to getting it working :)
What we did was copy the original react-native babel transformer from https://github.com/facebook/react-native/blob/master/packager/transformer.js into your project, and add the relay plugin:
plugins: [require('./babelRelayPlugin.js')],
babelRelayPlugin.js comes from https://github.com/relayjs/relay-starter-kit
I think my transform is working now. Thank you very much. @boourns
Getting a new Error: fetch is not a function which is very strange, because fetch is globally defined in react native(?).
Did you also encounter this error?