为什么不能导出PureComponent [英] Why cannot export a PureComponent

查看:83
本文介绍了为什么不能导出PureComponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用它:

export class FormAuto extends React.PureComponent {
   constructor(props) {
     ...
   }
  ....
}

我这样导入组件:从'./FormAuto'导入{FormAuto}

但是我想这样做,使用redux的 connect :

But I want to do it like this, to use connect from redux :

class FormAuto extends React.PureComponent {
   constructor(props) {
     ...
   }
  ....
}
export default connect(mapStateToProps, mapDispatchToProps)(FormAuto);

但是我有这个错误:

模块../../FormAuto没有导出的成员'FormAuto'

Module ../../FormAuto has no exported member 'FormAuto'

如果我这样导入组件:从'./FormAuto'导入FormAuto 该组件将不再显示.

If I import the composent like this : import FormAuto from './FormAuto' the component is no longer displayed.

你能帮我吗?

推荐答案

问题与 PureComponent 无关.这是因为您从命名导出更改为默认导出.

The problem isn't related to PureComponent. It is because you changed from a named export to a default export.

您有两个选择.

  1. 使用默认导入

import FormAuto from './path/to/FormAuto'

  1. 进行命名导出:

// Either the const or the class will need to be renamed in this scenario
export const FormAuto = connect(mapStateToProps, mapDispatchToProps)(FormAuto);

最重要的是您需要使它们保持一致.

The bottom line is you need to keep them consistent.

默认

export default MyComponent

// The import name can be anything you choose. 
// For clarity it may be preferred to use the component name
import MyComponent from './MyComponent';

命名

export MyComponent

import { MyComponent } from './MyComponent';

这篇关于为什么不能导出PureComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆