欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

利用ES6语法重构React组件详解

程序员文章站 2023-10-23 18:20:23
一、创建组件 es6 class创建的组件语法更加简明,也更符合javascript。内部的方法不需要使用function关键字。 react.createclas...

一、创建组件

es6 class创建的组件语法更加简明,也更符合javascript。内部的方法不需要使用function关键字。

react.createclass

import react from 'react';
const mycomponent = react.createclass({
 render: function() {
 return (
  <div>以前的方式创建的组件</div>
 );
 }
});
export default mycomponent;

react.component(es6)

import react,{ component } from 'react';
class mycomponent extends component {
 render() {
 return (
  <div>es6方式创建的组件</div>
 );
 }
}
export default mycomponent;

二、属性

props proptypes and getdefaultprops

. 使用react.component创建组件,需要通过在constructor中调用super()将props传递给react.component 。另外react 0.13之后props必须是不可变的。

. 由于是用es6 class语法创建组件,其内部只允许定义方法,而不能定义属性,class的属性只能定义在class之外。所以proptypes要写在组件外部。

. 对于之前的getdefaultprops方法,由于props不可变,所以现在被定义为一个属性,和proptypes一样,要定义在class外部。

react.createclass

import react from 'react';
const mycomponent = react.createclass({
 proptypes: {
 nameprop: react.proptypes.string
 },
 getdefaultprops() {
 return {
  nameprop: ''
 };
 },
 render: function() {
 return (
  <div>以前的方式创建的组件</div>
 );
 }
});
export default mycomponent;

react.component(es6)

import react,{ component } from 'react';
class mycomponent extends component {
 constructor(props) {
 super(props);
 }
 
 render() {
 return (
  <div>es6方式创建的组件</div>
 );
 }
}
mycomponent.proptypes = {
 nameprop: react.proptypes.string
};
mycomponent.defaultprops = {
 nameprop: ''
};
export default mycomponent;

三、状态

. 使用es6 class语法创建组件,初始化state的工作要在constructor中完成。不需要再调用getinitialstate方法。这种语法更加的符合javascript语言习惯。

react.createclass

import react from 'react';
const mycomponent = react.createclass({
 getinitialstate: function() {
 return { data: [] };
 },
 
 render: function() {
 return (
  <div>以前的方式创建的组件</div>
 );
 }
});
export default mycomponent;

react.component(es6)

import react,{ component } from 'react';
class mycomponent extends component {
 constructor(props) {
 super(props);
 this.state = { data: [] };
 }
 
 render() {
 return (
  <div>es6方式创建的组件</div>
 );
 }
}
export default mycomponent;

四、this

. 使用es6 class语法创建组件, class中的方法不会自动将this绑定到实例中。必须使用 .bind(this)或者 箭头函数 =>来进行手动绑定。

react.createclass

import react from 'react';
const mycomponent = react.createclass({
 handleclick: function() {
 console.log(this);
 },
 render: function() {
 return (
  <div onclick={this.handleclick}>以前的方式创建的组件</div>
 );
 }
});
export default mycomponent;

react.component(es6)

import react,{ component } from 'react';
class mycomponent extends component {
 handleclick() {
 console.log(this);
 }
 
 render() {
 return (
  <div onclick={this.handleclick.bind(this)}>es6方式创建的组件</div>
 );
 }
}
export default mycomponent;

也可以将绑定方法写到constructor中:

import react,{ component } from 'react';
class mycomponent extends component {
 constructor(props) {
 super(props);
 this.handleclick = this.handleclick.bind(this);
 }
 handleclick() {
 console.log(this);
 }
 
 render() {
 return (
  <div onclick={this.handleclick}>es6方式创建的组件</div>
 );
 }
}
export default mycomponent;

或者使用箭头函数 => :

import react,{ component } from 'react';
class mycomponent extends component {
 handleclick = () => {
 console.log(this);
 }
 
 render() {
 return (
  <div onclick={this.handleclick}>es6方式创建的组件</div>
 );
 }
}
export default mycomponent;

五、mixins

. 使用es6语法来创建组件是不支持react mixins的,如果一定要使用react mixins就只能使用react.createclass方法来创建组件了。

import react,{ component } from 'react';
var setintervalmixin = {
 dosomething: function() {
 console.log('do somethis...');
 },
};
const contacts = react.createclass({
 mixins: [setintervalmixin],
 
 handleclick() {
 this.dosomething(); //使用mixin
 },
 render() {
 return (
  <div onclick={this.handleclick}></div>
 );
 }
});
export default contacts;

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,如果有疑问大家可以留言交流。