Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require JSX in React-Native

How do you require JSX in React Native?

var MyScreen = require('./apps/screens/LoadingScreen');

Requiring a .js file this way works, but a .jsx file is not found.

like image 374
Marc Avatar asked Apr 23 '26 01:04

Marc


2 Answers

I think you may just have to rename your file from .jsx to .js and the build process will take care of any conversion. Colin's answer would seem to confirm this.

An example of JSX in a .js file is shown in http://moduscreate.com/react-native-has-landed/ .

The React team continues to encourage the use of JSX: http://facebook.github.io/react/docs/jsx-in-depth.html .

like image 68
David Aiken Avatar answered Apr 25 '26 15:04

David Aiken


Here's how I solved it with a quick hack: open [YOU_PROJECT_FOLDER]/node_modules/react-native/packager/react-packager/src/DependencyResolver/DependencyGraph/index.js and edit line 55 to look like this

extensions: extensions || ['jsx', 'js', 'json'],

the open [YOU_PROJECT_FOLDER]/node_modules/react-native/packager/react-packager/src/DependencyResolver/DependencyGraph/ResolutionRequest.js and after line 333 add these 2 lines:

  } else if (this._fastfs.fileExists(potentialModulePath + '.jsx')) {
    file = potentialModulePath + '.jsx';

This will make the packager recognize mymodule.jsx files when you do something like require("mymodule")

Note: the actual lines are from the latest 0.17 release, in other releases the lines may differ

like image 44
Razvan Pocaznoi Avatar answered Apr 25 '26 13:04

Razvan Pocaznoi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!