我正在this article之后创建一个使用命令npm install -g react-native-create-library
的react native库。我的库非常简单,它所做的只是弹出警报,并且索引文件中仅包含以下代码:
import { NativeModules } from 'react-native';
const { RNReactNativeOgonModule } = NativeModules;
class RNReactNativeOgon {
create() {
Alert.alert('You need to...')
}
}
export default new RNReactNativeOgon()
然后,我尝试使用npm pack
然后我将上述创建的包安装在
这样的react-native应用程序中npm install C:\Users\Java\Documents\CodeHub\ReactNative\ReactNativeOgon\react-native-react-native-ogon-1.0.0.tgz
然后将其导入到要使用的文件中,如下所示:
import React, {Component} from 'react';
import {Platform, Alert, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import {RNReactNativeOgon} from 'react-native-react-native-ogon';
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() {
const showAlert = () =>{
RNReactNativeOgon.create();
}
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native</Text>
<Text style={styles.instructions}>To get started, edit App.js</Text>
<Text style={styles.instructions}>{instructions}</Text>
<TouchableOpacity onPress ={showAlert} style = {styles.button}>
<Text>Alert</Text>
</TouchableOpacity>
</View>
);
}
}
我收到错误
> undefined is not an object (evaluating 'new_reactNativeReactNativeOgon.RNReactNativeOgon.create)
任何时候我单击触发该功能的TouchableOpcaity小部件:
const showAlert = () =>{
new RNReactNativeOgon.create();
}
表示import {RNReactNativeOgon} from 'react-native-react-native-ogon';
返回一个未定义的对象。
拜托,我在做什么错了?
答案 0 :(得分:2)
您是否尝试过:
import RNReactNativeOgon from 'react-native-react-native-ogon';
由于您的媒体库具有默认导出功能,因此您必须在导入时不要使用大括号{ }