React.Component是导出时的默认扩展吗? [英] Is React.Component the default extension when exporting?

查看:269
本文介绍了React.Component是导出时的默认扩展吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一些React项目,有时会看到 -

I am looking through some React projects, and sometimes see-

export default()=> {

但其他时候我看到 -

But other times I see-

export default class Entry extends React.Component {

两者之间有什么区别,导出自动扩展 React.Component ?什么是最佳做法?

Is there any difference between the two, viz. does export automatically extend React.Component? What is the best practice?

推荐答案

export default()=> 你看是一个React 0.14+ 功能组件

The export default () => you see is a React 0.14+ "Functional Component".

这是编写React组件的更简洁的语法。它和其他语法都很好。

It's a new more concise syntax for writing React components. Both it and the other syntax are fine.


这些组件的行为就像一个只有一个render方法定义的React类。由于没有为功能组件创建组件实例,所以添加到其中的任何参数都将被归零。功能组件没有生命周期方法,但您可以将.propTypes和.defaultProps设置为函数的属性。

These components behave just like a React class with only a render method defined. Since no component instance is created for a functional component, any ref added to one will evaluate to null. Functional components do not have lifecycle methods, but you can set .propTypes and .defaultProps as properties on the function.

基本上做: / p>

Basically doing:

class MyComponent extends React.Component {
  render() {
    return <p>Hello</p>;
  }
}

与以下相同:

const MyComponent = () => <p>Hello</p>;

在React组件中使用并传递给Render时。

When used inside React component and passed to Render.

这篇关于React.Component是导出时的默认扩展吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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