Uncaught ReferenceError: React is not defined

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

I am trying to make ReactJS work with rails using this tutorial. I am getting this error:


Uncaught ReferenceError: React is not defined

But I can access the React object in browser console


I also added public/dist/turbo-react.min.js as described here and also added //= require components line in application.js as described in this answer to no luck. Additionally,
var React = require('react') gives the error:
Uncaught ReferenceError: require is not defined

Can anyone suggest me on how to resolve this?

[EDIT 1]
Source code for reference:
This is my comments.js.jsx file:

var Comment = React.createClass({   render: function () {     return (       

{this.props.author}

{this.props.comment}
); } }); var ready = function () { React.renderComponent( , document.getElementById('comments') ); }; $(document).ready(ready);

And this is my index.html.erb:

回答1:

I was able to reproduce this error when I was using webpack to build my javascript with the following chunk in my webpack.config.json:

externals: {     'react': 'React' },

This above configuration tells webpack to not resolve require('react') by loading an npm module, but instead to expect a global variable (i.e. on the window object) called React. The solution is to either remove this piece of configuration (so React will be bundled with your javascript) or load the React framework externally before this file is executed (so that window.React exists).



回答2:

Possible reasons are 1. you didn't load React.JS into your page, 2. you loaded it after the above script into your page. Solution is load the JS file before the above shown script.

P.S

Possible solutions.

  1. If you mention react in externals section inside webpack configuration, then you must load react js files directly into your html before bundle.js
  2. Make sure you have the line import React from 'react';


回答3:

If you are using Webpack, you can have it load React when needed without having to explicitly require it in your code.

Add to webpack.config.js:

plugins: [         new webpack.ProvidePlugin({             "React": "react",         }),   ],

See http://webpack.github.io/docs/shimming-modules.html#plugin-provideplugin



回答4:

I got this error because I was using

import ReactDOM from 'react-dom'

without importing react, once I changed it to below:

import React from 'react'; import ReactDOM from 'react-dom';

The error was solved :)



回答5:

Adding to Santosh :

You can load React by

import React from 'react'


回答6:

import React, { Component, PropTypes } from 'react';

This may also work!



回答7:

I got this error because in my code I misspelled a component definition with lowercase react.createClass instead of uppercase React.createClass.



回答8:

The displayed error

after that import react

Finally import react-dom

if error is react is not define,please add ==>import React from 'react';

if error is reactDOM is not define,please add ==>import ReactDOM from 'react-dom';



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!