Javascript React JSX 文件给出错误“无法读取未定义的属性‘createElement’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39423054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React JSX file giving error "Cannot read property 'createElement' of undefined"
提问by Some Guy
I have a file test_stuff.js that I am running with npm test
我有一个正在运行的文件 test_stuff.js npm test
It pretty much looks like this:
它看起来像这样:
import { assert } from 'assert';
import { MyProvider } from '../src/index';
import { React } from 'react';
const myProvider = (
<MyProvider>
</MyProvider>
);
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal(-1, [1,2,3].indexOf(4));
});
});
});
Unfortunately, I get the error
不幸的是,我收到错误
/Users/me/projects/myproj/test/test_stuff.js:11
var myProvider = _react.React.createElement(_index.MyProvider, null);
^
TypeError: Cannot read property 'createElement' of undefined
at Object.<anonymous> (/Users/me/projects/myproj/test/test_stuff.js:7:7)
What does that mean? I am importing React from 'react' successfully, so why would React be undefined? It is _react.React, whatever that means...
这意味着什么?我成功地从 'react' 导入了 React,那么为什么 React 是未定义的?它是 _react.React,不管这意味着什么......
回答by Kafo
To import React do import React from 'react'
You add brackets when the thing you are importing is not the default export in that module or file. In case of react, it's the default export.
导入 React doimport React from 'react'
当您导入的内容不是该模块或文件中的默认导出时,您添加括号。在反应的情况下,它是默认导出。
This might apply to your other imports depending on how you defined them.
这可能适用于您的其他导入,具体取决于您如何定义它们。
回答by TJ Allen
import React, { Component } from 'react'
This worked for me. I'm not sure why it fixed my version of this issue, though. So if you are someone who stumbled upon this problem and you use create-react-app as your starting boilerplate, this way of importing React will do the trick. (as of Oct '18, lol)
这对我有用。不过,我不确定为什么它修复了我的这个问题版本。因此,如果您偶然发现了这个问题,并且您使用 create-react-app 作为您的起始样板,那么这种导入 React 的方式就可以解决问题。(截至 18 年 10 月,哈哈)
回答by Khuong
For those who are working ReactJS with TypeScript.
对于那些使用 TypeScript 使用 ReactJS 的人。
import * as React from 'react';
回答by Saran M S
Change: import { React } from 'react'to import React from 'react'Because React is a default export and you don't need curly braces for any default exports.
更改: import { React } from 'react'到 import React from 'react'因为 React 是默认导出,您不需要任何默认导出的花括号。
回答by Sasi Kumar M
If in case you need to import multiple classes from 'react', you can have an alias for them except React. Something like,
如果您需要从“react”导入多个类,您可以为它们指定一个别名,除了 React。就像是,
import React, * as react from 'react';
回答by Vimal Kurien Sam
This error occured to me due to carelessness. It's actually
由于粗心大意,我发生了这个错误。其实是
import React from 'react';
Brackets are for named exports such as this:
括号用于命名导出,例如:
import React, { useState, useEffect } from 'react';