简体   繁体   English

导出React组件最合理的方法是什么?

[英]What is the most logical way to export React components?

When writing React components in ES6, I sometimes wonder what the most logical way would be to export a component. 在ES6中编写React组件时,我有时想知道导出组件最合乎逻辑的方式。 I have found three ways to do so, and I mostly employ the first one: 我找到了三种方法,我主要使用第一种:

export default class 导出默认类

export default class extends Component {
    render() {
        //
    }
}

Seems to the most straightforward way. 似乎是最直接的方式。 The only downside I can see is that the component is not explicitly named in the file except for the filename. 我能看到的唯一缺点是除文件名外,组件没有在文件中明确命名。

export default class ComponentName 导出默认类ComponentName

export default class ComponentName extends Component {
    render() {
        //
    }
}

Seems to be practically the same as above, apart from the class being named explicitly. 除了明确命名的类之外,似乎与上面几乎相同。 Side question: Is there a difference between the two above when importing the component? 附带问题:导入组件时,上述两者之间是否存在差异?

class with separate export 具有单独导出的类

class ComponentName extends Component {
    render() {
        //
    }
}

export default ComponentName

I think this also is programmatically identical to the other two examples, but I'm unsure. 我认为这在编程上与其他两个例子相同,但我不确定。

Does employing one of these three examples have a notable benefit over the others? 使用这三个例子中的一个是否比其他例子有显着的好处?

You should pick the third option since it's cleaner to wrap your component. 您应该选择第三个选项,因为它更清洁以包装您的组件。 Example: 例:

class ComponentName extends Component {
    render() {
        //
    }
}

export default connect(null)(ComponentName);

export default class 导出默认类

export default class extends Component { ... }

Problems with this: 这个问题:

  • You won't be able to refer to the class from within the class - so won't be able to access its static properties 您将无法从类中引用该类 - 因此将无法访问其静态属性

  • You also won't be able to define static properties on this class from outside of it which is often what you want to do with defaultProps and propTypes 您也将无法在此类之外定义此类的静态属性,这通常是您要对defaultPropspropTypes

  • In case something within the class errors it will be harder to debug because the class is unnamed so it won't show up in the stack traces 如果类中的某些内容出现错误,则调试将更加困难,因为该类未命名,因此它不会显示在堆栈跟踪中


export default class ComponentName 导出默认类ComponentName

export default class ComponentName extends Component { ... }

( Benefits of this: You avoid all of the above problems and it's short and concise. ) (这样做的好处:您可以避免上述所有问题,并且简洁明了。)

Problems with this: 这个问题:

  • If you want to wrap your component in a Higher Order Function such as with Redux's connect it can make the class signature really long making it hard to read 如果你想把你的组件包装在一个更高阶的函数中,比如使用Redux的连接,它会使类签名真的很长,这使得它很难阅读

class with separate export 具有单独导出的类

class ComponentName extends Component { ... }

export default ComponentName

Benefits of this: 这样做的好处:

  • You avoid all of the above problems 你避免了上述所有问题

我使用带有单独导出的类,因为它对我来说感觉更干净,特别是当你需要使用redux connect,material-ui样式和许多库包装你的组件时。

These export methods do pretty much the same thing all around. 这些导出方法几乎完全相同。 I personally think it comes down to consistency (you want to use the same method throughout your app) and I prefer using component names as I can then use things like instanceOf 我个人认为这取决于一致性(你想在整个应用程序中使用相同的方法),我更喜欢使用组件名称,因为我可以使用像instanceOf这样的东西

Also if I am using redux I prefer the last method (separate export statement) to use connect. 另外,如果我使用redux,我更喜欢使用connect的最后一个方法(单独的export语句)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM