如何要求无状态功能组件(未知模块_createWrapper?)
How to require Stateless Functional Components (Unknown module _createWrapper?)
我正在尝试学习如何使用无状态功能组件,并且这样要求它:
const Button = require('./components/Button');
在Button.js中:
import React from 'react';
const { Text, View, TouchableOpacity,} = ReactNative;
const Button = () => {
return (
<View><Text>Button</Text></View>
);
}
export default Button;
这给了我:
Requiring unknown module "./_createWrapper".If you are sure the module
is there, try restarting the packager or running "npm install".
如果我像这样在线执行它,它会起作用:
const Button = () =>
<View><Text>Button</Text></View>
但由于我想将其保留为可重用组件,因此我希望将其保存在一个文件中。我该怎么办?
require是commonJs导入语法,export默认是ES6导出语法。
所以坚持使用 commonjs 或 ES6 模块。
在 Button.js 导出时
使用module.exports = Button;
或者您可以在导入时使用 es6 import 语句
import Button from './components/Button'
我正在尝试学习如何使用无状态功能组件,并且这样要求它:
const Button = require('./components/Button');
在Button.js中:
import React from 'react';
const { Text, View, TouchableOpacity,} = ReactNative;
const Button = () => {
return (
<View><Text>Button</Text></View>
);
}
export default Button;
这给了我:
Requiring unknown module "./_createWrapper".If you are sure the module is there, try restarting the packager or running "npm install".
如果我像这样在线执行它,它会起作用:
const Button = () =>
<View><Text>Button</Text></View>
但由于我想将其保留为可重用组件,因此我希望将其保存在一个文件中。我该怎么办?
require是commonJs导入语法,export默认是ES6导出语法。
所以坚持使用 commonjs 或 ES6 模块。
在 Button.js 导出时
使用module.exports = Button;
或者您可以在导入时使用 es6 import 语句
import Button from './components/Button'