Issue
React Native app fails to resolve components.
I am trying to import & render Test.jsx inside of App.js.
I get the following error-
error: bundling failed: Error: Unable to resolve module `./screens/Test` from App.js`:
The module `./screens/Test` could not be found from App.js. Indeed, none of these files exist
Project Manager(or rather files) look like this-
Code of Test.js-
import React, { Component } from 'react';
import {View, Text, StyleSheet} from 'react-native'
export default class Test extends Component {
render() {
return (
<View style={styles.container}>
<Text>Hello World?</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
}
});
Code of App.js-
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View
} from 'react-native';
import Test from "./screens/Test";
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
type Props = {};
export default class App extends Component<Props> {
render() {
return (
<View style={styles.container}>
<Test />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
I've literally tried every solution mentioned - react-native#4968 and none of them seem to work. I've also referred to this, but, the solution is for css and nowhere close to what would fix this issue.
I've looked at lot more issues and SO's with no clue what else I have to do. I've also created a new project(screenshot and code is from the new project).
What am I missing?
UPDATE:
I realised that the issue was because I have .jsx extension. Imports are working fine for .js file. Any pointers on how to enable the project to accept .jsx for imports?
Solution
update: for RN >0.59 as @RedGaint pointed in https://stackoverflow.com/a/55134051/8034782 you need to configure metro.config.js in the root level.
module.exports = {
resolver: {
/* resolver options */
sourceExts: ['jsx','js'] //add here
},
transformer: {
/* transformer options */
},
serializer: {
/* serializer options */
},
server: {
/* server options */
}
/* general options */
};
For RN < 0.57: Inside of the root of your project add a file named rn-cli.config.js and place the following code into it.
// ./rn-cli.config.js
module.exports = {
/// @description Allows you to enable support for JSX files
/// The `index.js` file in the root of your project has to be `.js`.
getSourceExts: () => [ 'jsx', 'js' ],
}
For RN > 0.57:
module.exports = {
resolver: {
sourceExts: ['jsx', 'js'],
},
};
for more reference look into this there is already an issue opened:
https://github.com/facebook/react-native/pull/5233#issuecomment-382083236
Answered By - aravind_reddy

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.