React Native iOS自定义包调用_reactNativeSpotLib.show方法报错求助
TypeError: null is not an object (evaluating '_reactNativeSpotLib.default.show') in React Native iOS Custom Module Hey there, let’s break down why you’re hitting this error and walk through actionable fixes. This error almost always means your custom native popup module isn’t being properly exposed, linked, or imported in your React Native code on iOS. Here’s how to troubleshoot step by step:
1. Verify Your iOS Native Module Implementation
First, double-check that your native code correctly exposes the show method to React Native:
- In your module’s
.hfile, ensure it conforms toRCTBridgeModule:// ReactNativeSpotLib.h #import <React/RCTBridgeModule.h> @interface ReactNativeSpotLib : NSObject <RCTBridgeModule> @end - In your
.mfile, confirm you’ve exported the module and theshowmethod:
Make sure// ReactNativeSpotLib.m #import "ReactNativeSpotLib.h" @implementation ReactNativeSpotLib // Export the module to React Native RCT_EXPORT_MODULE(); // Expose the show method to JS RCT_EXPORT_METHOD(show) { // Your popup logic here (example alert) UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Demo Popup" message:@"This is your custom popup!" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; // Present the alert from the root view controller UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController; [rootVC presentViewController:alert animated:YES completion:nil]; } @endRCT_EXPORT_MODULE()doesn’t have a custom name unless you’re explicitly using that name in JS.
2. Confirm Proper Linking (React Native 0.60+)
Auto-linking should handle most cases, but manual checks can catch issues:
- Open your iOS project in Xcode, then go to your target’s Build Phases:
- Check that
ReactNativeSpotLib.mis listed in Compile Sources. - Ensure
libReactNativeSpotLib.aappears in Link Binary With Libraries (if using a static library setup).
- Check that
- Run
cd ios && pod install && cd ..to refresh CocoaPods dependencies, then restart your Metro server.
3. Fix Your JS Import Logic
This is one of the most common mistakes! Native modules aren’t imported as default exports—you need to grab them from NativeModules:
// ❌ Wrong: This will return null if no default export exists // import ReactNativeSpotLib from 'react-native-spot-lib'; // ✅ Correct: Use NativeModules to access the native module import { NativeModules } from 'react-native'; const { ReactNativeSpotLib } = NativeModules; // Then call the method safely const triggerPopup = () => { if (ReactNativeSpotLib?.show) { ReactNativeSpotLib.show(); } else { console.warn("Native module not available"); } };
If you added TypeScript types for your module, ensure they correctly map to the NativeModules structure.
4. Clear Caches and Rebuild
Stale cache often causes modules to not be detected:
- Stop your Metro server, then run
npx react-native start --reset-cacheto restart with a fresh cache. - In Xcode, use Cmd + Shift + K to clean the build folder, then recompile and run your iOS app.
5. Check iOS Project Configuration
- Ensure your iOS project’s Deployment Target matches React Native’s minimum requirement (usually iOS 11.0 or higher).
- If your module relies on other native dependencies, confirm those are also linked correctly in your project.
Start with verifying the native module implementation and JS import—those are the most frequent causes. If you’re still stuck, share a snippet of your native code or JS import, and we can dig deeper!
内容的提问来源于stack exchange,提问作者Aqib Mirza

