我有一个 RN 应用程序(RN 版本 0.48.3),我的文件夹结构:
/index.ios.js
/index.android.js
/app/index.js
index.ios.js:
import {AppRegistry} from 'react-native';
import home from './app';
AppRegistry.registerComponent('home', () => home);
/app/index.js:
import React, { Component } from 'react';
import {
AppRegistry,
View
} from 'react-native';
export default class home extends Component {
render() {
return (
<View>
...
</View>
);
}
}
如果我运行该应用程序,它会显示此错误:
Element type is invalid: expected a string (for Built-in components) or a class/Object...
但是当更改index.ios.js 中的第二行时,它可以正常工作:
import home from './app'; -> import home from './app/index.js';
谁能帮我解释一下为什么?路径“./app”里面的index.js不正确?
最佳答案
尝试使用驼峰命名的 Home,并从你的类名中删除 home
瞧:
import React, { Component } from 'react';
import {
AppRegistry,
View
} from 'react-native';
export default class extends Component {
render() {
return (
<View>
...
</View>
);
}
}
import {AppRegistry} from 'react-native';
import Home from './app';
AppRegistry.registerComponent('Home', () => Home);
关于javascript - React-native 导入路径快捷方式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46651337/