首页 > 解决方案 > 为 React Native 创建 NPM 包时如何解决 iOS 中出现的问题?

问题描述

我从这里开始关注这篇文章来创建反应原生库。我已经发布了这个库并在我的本地项目中使用了它。它在 android 中运行良好,但是当我尝试在 iOS 上运行它时,它会引发如下错误:

TypeError:null 不是对象(评估 '_exampleBridge.default.showMessage')。

未捕获的错误

我已经在 xcode13 中打开了库项目,它构建成功。
提前致谢。

import React from 'react';
import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, useColorScheme, View, Button } from 'react-native';
import {Colors,Header} from 'react-native/Libraries/NewAppScreen';
import RNExampleBridge from 'example-bridge-1.0.8'

const App = () => {
  const isDarkMode = useColorScheme() === 'dark';

  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  function showToastAlert(){
    RNExampleBridge.showMessage()
  }

  return (
    <SafeAreaView style={backgroundStyle}>
      <StatusBar barStyle={isDarkMode ? 'light-content' : 'dark-content'} />
      <ScrollView
        contentInsetAdjustmentBehavior="automatic"
        style={backgroundStyle}>
        <Header />
        <View
          style={{
            backgroundColor: isDarkMode ? Colors.black : Colors.white,
          }}>
          <View>
            <Button title="Show Native Message" onPress={() => { showToastAlert() }}/>
          </View>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
};

export default App;

标签: javascriptiosreact-nativenpmnpm-publish

解决方案


推荐阅读