【问题标题】:Error while evaluating JSX element in React Native在 React Native 中评估 JSX 元素时出错
【发布时间】:2016-07-05 07:51:18
【问题描述】:

运行我的应用程序时,当 babel 编译器处理 JSX 时出现错误。这可能是由于.babelrc 中的错误配置造成的。这是错误:

undefined 不是对象(评估 '_react.React.createElement')

Screenshot

  • 为 babel 添加 transform-react-jsx 插件并不能解决问题

index.js:

renderLoadingView() {
  return (
    <View style={styles.container}>  // Failing on index.js:217
      <Text>
        Loading the app...
      </Text>
    </View>
  );
}

.babelrc:

{
  "presets": ["react-native-stage-0"]
}

【问题讨论】:

  • 会不会是因为您忘记了文件中的import React from 'react'? JSX 似乎不是问题;问题是 React.createElement 未定义,最可能的原因是未导入它
  • 乍一看好像是被导入的。在我的index.js 中导入 React 的行如下import { React, Component } from 'react';
  • 应该是import React, { Component } from 'react'
  • 是的,你是对的。它解决了我的问题!

标签: reactjs react-native babeljs react-jsx jsx


【解决方案1】:

如果你使用 JSX 则必须导入 React,所以你需要编写

import React from 'react';

在文件的顶部。

【讨论】:

    猜你喜欢
    • 2021-10-25
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 2021-05-17
    相关资源
    最近更新 更多