简体   繁体   English

React.createClass和之间的区别 <Name> 扩展组件

[英]Difference between React.createClass and <Name> extends Component

I've been through several tutorials and guides for React Native and have seen these two being used in almost the same way: 我已经看过关于React Native的一些教程和指南,并且看到这两种用法几乎相同:

var anyName = React.createClass({
  render: function() {
   return (
     ...
   );
  }
});

and

class anyName extends Component {
  render() {
   return (
     ...
   )
  }
}

Not sure what the difference between them is. 不知道它们之间有什么区别。

The first piece of code creates new JS object in old way (ES5). 第一部分代码以旧方式 (ES5)创建新的JS对象。 It simply creates plain JS object and then fill it with React methods and properties using prototypes . 它只是创建一个普通的JS对象,然后使用prototypes使用React方法和属性填充它。

React.createClass method must be filled with object that implements render method. React.createClass方法必须填充实现render方法的对象。 (You can read more about this method here . (您可以在此处阅读有关此方法的更多信息。

The second code uses newest JS standard (ES6) classes and inheritance . 第二个代码使用最新的JS标准(ES6) 继承 In fact Component is ReactComponent and you can read more about it here . 实际上ComponentReactComponent ,您可以在这里阅读有关它的更多信息。

Please note that these two ways of initializing React Component will give you sightly different results. 请注意,这两种初始化React Component的方式将给您明显不同的结果。 For example you cannot use mixins at this time with ES6 inheritance and you have to bind methods to parent object. 例如,您此时不能将mixins用于ES6继承,而必须bind方法bind到父对象。 You can read more about differences here . 您可以在此处阅读有关差异的更多信息。

暂无
暂无

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

相关问题 用扩展的React.Component代替React.createClass - Replacing a React.createClass with extends React.Component 尝试将React.CreateClass转换为扩展React.Component - Trying to convert React.CreateClass to extends React.Component 具有React.createClass的组件中的SyntaxError - SyntaxError in component with React.createClass React / ES6导出createClass和Extended Component之间的区别 - React/es6 difference between exports createClass and extends Component 在创建React组件时,我应该使用功能组件,React.createClass还是扩展React.Component? - When creating React components should I use functional components, React.createClass or extends React.Component? 如何将代码从“ React.createClass”更改为“ NoteNote类扩展组件”并使之工作 - How to change code from `React.createClass` to `class NoteNew extends Component` and make it work React.js:使用React.render()和React.createClass()的渲染传递子组件之间的区别? - React.js: Difference between passing in the subcomponents with React.render() and with React.createClass()'s render? 如何扩展通过React.createClass创建的React组件类 - How to extend React component class created via React.createClass 在React.js中创建组件:“ React.createClass”是否仍然可用? - Creating a component in React.js: is “React.createClass” still usable? 如何将 React.createclass 转换为 Class 组件? - How Can I convert React.createclass to Class Component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM