DylanVann / react-native-fast-image

🚩 FastImage, performant React Native image component.
MIT License
8.1k stars 1.48k forks source link

Expo web: "export 'requireNativeComponent' was not found in 'react-native-web/dist/index' #884

Open koaladlt opened 2 years ago

koaladlt commented 2 years ago

Describe the bug Every time that I run the app on the web with expo, this error comes in.

To Reproduce Steps to reproduce the behavior if possible, or a link to a reproduction repo:

  1. Build your expo app on the web

Expected behavior I need the app to compile, I'm not sure if React-Native-Fast-Image is available for React Native Web. That would not be a problem at first, but this error would not let the app compile.

Screenshots Screen Shot 2022-03-15 at 15 08 08

Dependency versions

haveamission commented 2 years ago

From what it looks like, it is not available for React Native Web. Maybe some sort of transpiling would work? Alternatively, you could put a wrapper around it that exports for RNW and for RN iOS and Android. I've had to do that for a few packages that weren't supported on either platform.

I do wish there was some sort of automatic option here though - I have a RNW build and was interested in possibly using FastImage for caching.

koaladlt commented 2 years ago

@haveamission Thanks for your answer, your approach looks interesting.

I will try that and let you know here if that worked!

jesse-savary commented 2 years ago

@haveamission @koaladlt Here is a patch I wrote some time ago to do this:

diff --git a/dist/index.js b/dist/index.js
index 1fc0e9d441836b8086119adc56701ac44539df16..1ac9d0ba09e31f840c717aed3c8f6a24194091e4 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1,6 +1,6 @@
 import _extends from '@babel/runtime/helpers/extends';
 import React, { forwardRef, memo } from 'react';
-import { NativeModules, StyleSheet, requireNativeComponent, Image, View } from 'react-native';
+import { NativeModules, StyleSheet, Image, View, Platform } from 'react-native';

 const FastImageViewNativeModule = NativeModules.FastImageView;
 const resizeMode = {
@@ -98,14 +98,21 @@ const styles = StyleSheet.create({
   }
 }); // Types of requireNativeComponent are not correct.

-const FastImageView = requireNativeComponent('FastImageView', FastImage, {
-  nativeOnly: {
-    onFastImageLoadStart: true,
-    onFastImageProgress: true,
-    onFastImageLoad: true,
-    onFastImageError: true,
-    onFastImageLoadEnd: true
-  }
-});
+let FastImageView
+
+if (Platform.OS === 'web') {
+  FastImageView = Image
+} else {
+  const { requireNativeComponent } = require('react-native')
+  FastImageView = requireNativeComponent('FastImageView', FastImage, {
+    nativeOnly: {
+      onFastImageLoadStart: true,
+      onFastImageProgress: true,
+      onFastImageLoad: true,
+      onFastImageError: true,
+      onFastImageLoadEnd: true,
+    },
+  })
+}

 export default FastImage;
globemediaofficial commented 1 year ago

@jesse-savary How do I use this patch? I'm new to React Native and haven't needed to learn this yet.

adderly commented 1 year ago

The patch for the 8.6.3, using the tool patch-package :

https://gist.github.com/adderly/9cf1b5c2d6f51365a870c7ac70388c9a