React App Error' Uncaught ReferenceError:React未定义'

时间:2016-03-28 19:32:35

标签: javascript reactjs npm gruntjs browserify

我正在尝试使用react构建一个应用程序,但它不会工作,我无法理解为什么。所有东西都使用babelify进行编译,但它在性能期间会抛出异常。错误如下:未捕获的ReferenceError:未定义React。

这是我的组件文件(BigCard.jsx):

}, {
  "status": "OK",
  "duration": {
    "value": 24487,
    "text": "6 heures 48 minutes"
  },
  "distance": {
    "value": 129324,
    "text": "129 km"
  }

我的主文件(main.js):

 export var BigCard = React.createClass({
  render: function() {
    var rows = [];
    for (var variable in this.props.pokemon) {
      if (this.props.pokemon.hasOwnProperty(variable) && variable.toString() !== 'id' && variable.toString !== 'name' && variable.toString !== 'image' && variable.toString !== 'types') {
        rows.push(
          <tr>
            <td class='mdl-data-table__cell--non-numeric'>
              {variable}
            </td>
            <td>
              {this.props.pokemon[variable]}
            </td>
          </tr>
        )
      }
    }
    return (
      <div class='mdl-card mdl-shadow--4dp'>
        <div class='mdl-card__title'>
          <img src={this.props.pokemon.image.src} alt='Pokemon' class='bigCard__img'/>
        </div>
        <h2 class='mdl-card__title-text'></h2>
        <div class='mdl-card__supporting-text'>
          <table class='mdl-data-table mdl-js-data-table'>
            <thead>
              <tr>
                <th class='mdl-data-table__cell--non-numeric'>Type</th>
                <th class='mdl-data-table__cell--non-numeric'>{this.props.pokemon.types}</th>
              </tr>
            </thead>
            <tbody>
              {rows}
            </tbody>
          </table>
        </div>
      </div>
    );
  }
});

我的grunt设置文件(grunfile.js)(我删除了大部分命令,所以它在这里看起来更小,但是这个文件的所有内容都没问题):

import React from 'react';
import ReactDOM from 'react-dom';
import * as DataLogic from './modules/data-logic.js';
import SmallCard from './components/SmallCard.jsx';
import BigCard from './components/BigCard.jsx';

//Test Method
DataLogic.getPokemonById(3).then((result) => {
  ReactDOM.render(
    <BigCard pokemon={result} />,
    document.getElementById('bigCard')
  );
}).catch((error) => console.log(`${error} in main.js`));

我的包文件(package.json)(我也删除了大多数引用,但最重要的是那里):

'use strict'
module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);
  grunt.initConfig({
    pkg: grunt.file.readJSON('./package.json'),
    watch: {
      babelify: {
        files: ['./scripts/**/*.js','./blocks/**/*.js','./scipts/components/*.jsx'],
        tasks: ['browserify']
      },
    },
    eslint: {
      options: {
        format: require('eslint-tap'),
        configFile: '.eslintrc'
      },
      target: './scripts/**/*.js'
    },
    browserify: {
      dist: {
        options: {
          transform: [
            ['babelify', {
              presets: ['es2015','react']
            }]
          ],
          browserifyOptions: {
            debug: true
          },
          exclude: ''
        },
        files: {
          './build/main.js': ['./scripts/**/*.js','./blocks/**/*.js','./scipts/components/*.jsx']
        }
      }
    }
  });
  grunt.registerTask('default', ['browserify', 'eslint', 'jade', 'sass', 'cssmin','uglify','watch']);
};

也许,错误是显而易见的,但我无法看到它。你能帮帮我吗?

1 个答案:

答案 0 :(得分:7)

在BigCard.jsx中添加import React from 'react'

每个使用react的文件都需要包含import语句。