reactjs – React.Component和React.createClass

前端之家收集整理的这篇文章主要介绍了reactjs – React.Component和React.createClass前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我困惑了什么是组件和反应类之间的区别?

我什么时候在反应类上使用组件?
看起来组件是一个类,createClass创建一个组件。

https://facebook.github.io/react/docs/top-level-api.html

React.Component

This is the base class for React Components when they’re defined using
ES6 classes. See Reusable Components for how to use ES6 classes with
React. For what methods are actually provided by the base class,see
the Component API.

React.createClass

Create a component class,given a specification. A component
implements a render method which returns one single child. That child
may have an arbitrarily deep child structure. One thing that makes
components different than standard prototypal classes is that you
don’t need to call new on them. They are convenience wrappers that
construct backing instances (via new) for you.

他们是做同样的事情的两种方式。

React.createClass是一个返回一个Component类的函数

MyComponent = React.createClass({
  ...
});

React.Component是您可以扩展的现有组件。当使用ES6时主要有用。

MyComponent extends React.Component {
  ...
}
原文链接:https://www.f2er.com/react/302348.html

猜你在找的React相关文章