2

I am trying to build an app using react, but it won't work and I can't understand why. Everything compiles using babelify, but it throws an exception during performance. The error is following: Uncaught ReferenceError: React is not defined.

Here is my Component file (BigCard.jsx):

 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>
    );
  }
});

My main file (main.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`));

My grunt settings file (grunfile.js) (I deleted most of the commands, so it looks more tiny here, but everything's okay with that file):

'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']);
};

And my package file (package.json) (I also removed most of the refs, but most important are there):

{
  "name": "Pockedex",
  "version": "0.1.0",
  "dependencies": {
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-react": "^6.5.0",
    "babelify": "^7.2.0",
    "react": "^0.14.7",
    "react-dom": "^0.14.7"
  },
  "devDependencies": {
    "babel-preset-es2015": "6.3.x",
    "babelify": "7.2.x",
    "eslint-tap": "1.1.x",
    "grunt": "^0.4.5",
    "grunt-babel": "6.0.x",
    "grunt-browserify": "^4.0.1",
    "grunt-contrib-concat": "^1.0.0",
    "grunt-eslint": "^17.3.2",
    "load-grunt-tasks": "3.4.x"
  }
}

Perhaps, the error is obvious, but I just can't see it. Can you help me with that, please?

3
  • 1
    Do you have import React from 'react'; in BigCard.jsx? Commented Mar 28, 2016 at 19:39
  • @erichardson30 no, that's where the problem was, thanks! Commented Mar 28, 2016 at 19:41
  • please mark answer as correct if that fixes your issue Commented Mar 28, 2016 at 20:12

1 Answer 1

11

Add import React from 'react' in BigCard.jsx

Every file that uses react needs to have the import statement in it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.