[英]stateless functional components and `react` import
I face the same issue as described here https://github.com/babel/babel/issues/2504 我面临的问题与https://github.com/babel/babel/issues/2504中描述的相同
So, any file which has only stateless components needs to have react
imported like import React from 'react';
因此,任何只有无状态组件的文件都需要
react
导入,例如import React from 'react';
, which is bit annoying (eslint gives error as unused variable; which I understand can be suppressed, still..). ,这有点烦人(eslint给出错误作为未使用的变量;我理解可以抑制,仍然..)。 Is there a way to avoid having this import in a
webpack
based setup. 有没有办法避免在基于
webpack
的设置中进行此导入。
thanks. 谢谢。
I had the same issue. 我遇到过同样的问题。 Then, I found this:
然后,我发现了这个:
babel-plugin-react-require 巴别-插件反应的-需要
This will automatically add the required require
or import
calls to get 'react'
imported to your stateless component modules. 这将自动添加所需的
require
或import
调用,以将'react'
导入到无状态组件模块。
PS If you use webpack and babel6, ensure that you are not using the jsx-loader
for your JSX files. PS如果您使用webpack和babel6,请确保您没有将
jsx-loader
用于JSX文件。 I was getting errors with this and then I realized the jsx-loader
is not required anymore. 我得到了错误,然后我意识到不再需要
jsx-loader
。 Just use: 只需使用:
You can use Webpack's ProvidePlugin ( https://github.com/webpack/docs/wiki/shimming-modules#plugin-provideplugin ): 您可以使用Webpack的ProvidePlugin( https://github.com/webpack/docs/wiki/shimming-modules#plugin-provideplugin ):
new webpack.ProvidePlugin({
React: "react"
})
Now you'll have React available in every module without having to explicitly write import React from 'react'
现在,您可以在每个模块中使用React,而无需
import React from 'react'
显式写入import React from 'react'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.