简体   繁体   中英

Error when bundling for web from a React Native component using babel-plugin-react-native-web

I am following this tutorial to set up bundling for web from a React Native component.

I am successfully bundling using the webpack config from the tutorial, however when I import the exported Button into a React project and try and render it, I get the following error in the browser:

TypeError: Failed to construct 'Image': Please use the 'new' operator, this DOM object constructor cannot be called as a function.

renderWithHooks
./node_modules/react-dom/cjs/react-dom.development.js:14803

  14800 |   }
  14801 | }
  14802 | 
> 14803 | var children = Component(props, secondArg); // Check if there was a render phase update
        | ^  14804 | 
  14805 | if (workInProgress.expirationTime === renderExpirationTime) {
  14806 |   // Keep rendering in a loop for as long as render phase updates continue to

Below is the React Native component I am bundling to web:

import React from "react";
import { TouchableOpacity, StyleSheet, Text } from "react-native";

export default function Button({ onPress, imageSource }) {
  return (
    <TouchableOpacity style={styles.button} onPress={onPress}>
      <Text>
        A
        </Text>
    </TouchableOpacity>
  );
}

const styles = StyleSheet.create({
  container: {
    width: 56,
    height: 56,
    borderRadius: 28,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#fff",
  },
  image: {
    width: 28,
    height: 28,
  },
});

These are the versions of the packages I am using to bundle the React Native component with:

"babel-plugin-react-native-web": "^0.17.5",
"metro-react-native-babel-preset": "^0.66.2",
"webpack": "^5.64.2",
"webpack-cli": "^4.9.1",
"babel-loader": "^8.2.3",

I realised the problem is that the tutorial does not export Image , I'm now correctly exporting Image and it's working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM