6条给React开发者的专业建议

1. 使用方法组件

在你不需要使用组建的内部状态和生命周期方法
你可以使用

function welcome(props){
    return <h1>Hello,{props.name}</h1>;
}

来代替

class Welcome extends Component{
    render(){
        return <h1>Hello,{this.props.name}</h1>
    }
}
使用这种方法的好处:
  • 更少的代码,
  • 更容易理解,
  • 不用处理状态机,
  • 测试简单,
  • 不需要绑定this,
  • 更容易提取较小的组件

2.尽可能的让你的组件小巧

使用这种方法的好处:

  • 便于阅读
  • 便于测试
  • 方便维护
  • 方便复用

例如:下面我们把个人中心资料模块进行细分

个人用户信息部分被拆分

class Comment extends Compont{
    render()
     <div className="Comment">
        <UserInfo user={this.props.user}/>
        <div className='Comment-text'>
        {this.props.text}
        </div>
        <div className="comment-text">
        {formatDate(this.props.date)}
        </div>
    </div>
}

在用户信息部分中,头像部分也被拆分成更小的组件

function UserInfo(props){
    renturn (
    <div className="UserInfo">
        <Avatar user={props.name}/>
        <div classNmae='UserInfo-name'>
        {props.user.name}
        </div>
    </div>
    );
}

个人头像部分

function Avatar(props){
    return(
    <img className='Avatar'
        src={props.user.avatarUrl}
        alt={props.user.name}
    </img>
    );
}

3.了解并知道怎样处理'this'

1. 在render中绑定

这种方式虽然简单明了但是有性能问题,因为点击后会调用一个新的函数,每次这个组件都要重新渲染

class HelloWorld extend component{
    contructor(props){
        super(props);
        this.state={message:'Hi'};
    }
    logMessage(){
        console.log(this.state.message);
    }
    render(){
        return {
            <input type='button' value='Log' onClick={this.logMessage.bind(this)}/>
        }
    }
}

2. 在render中使用箭头函数

这种方式和方法1同样会有相同的性能问题

class HelloWorld extend component{
    contructor(props){
        super(props);
        this.state={message:'Hi'};
    }
    logMessage(){
        console.log(this.state.message);
    }
    render(){
        return {
            <input type='button' value='Log' onClick={()=>this.logMessage}/>
        }
    }
}

3. 在构造方法中绑定

这种方式需要记得使用super(props)

class HelloWorld extend component{
   contructor(props){
       super(props);
       this.state={message:'Hi'};
       this.logMessage= this.logMessage.bind(this)
   }
   logMessage(){
       console.log(this.state.message);
   }
   render(){
       return {
           <input type='button' value='Log' onClick={this.logMessage}/>
       }
   }
}

4. 在属性中使用箭头函数

有方法1和2的性能问题

class HelloWorld extend component{
   
   this.state={message:'Hi'};
    
   logMessage=()=>{
       console.log(this.state.message);
   }
   render(){
       return {
           <input type='button' value='Log' onClick={this.logMessage}/>
       }
   }
}

4.在更新状态时使用方法而不是对象

user:

this.setState((prevState.props)=>{
    renturn {correctData:!prevState.correctData}
});

not

this.setState({correctData:!this.state.correctData});

5.开发lib时使用'prop-types'

import PropTypes from 'pros-types';

class Welcome extends Component{
    render(){
        return <h1>Hello,{this.props.name}</h1>
        
    }
}
Welcome.propTypes={
    name:ProsTypes.string.isRequired
}

6.使用必要的React 开发工具来帮助我们

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 作为一个合格的开发者,不要只满足于编写了可以运行的代码。而要了解代码背后的工作原理;不要只满足于自己的程序...
    六个周阅读 12,695评论 1 33
  • 本笔记基于React官方文档,当前React版本号为15.4.0。 1. 安装 1.1 尝试 开始之前可以先去co...
    Awey阅读 12,314评论 14 128
  • 黛色入幽 展眉颦几度 雾纱轻盈 盈盈春水环绕 多情横波目 迤逦远行的梦 静静安放 几点杜鹃红 凝目处 听几声翠鸟 ...
    金钗银环阅读 4,292评论 105 112
  • 因为之前豪情万丈的说了,我要好好的训练写作。 所以,才有了这一篇文章,碍于字数,有可能成为连载。 本文纯粹从一个逗...
    linachai阅读 1,807评论 1 0
  • 在北方上学后,明显不适应家里没暖气的日子,更不喜欢老不放晴的天。 曾经对毕业后的生活有诸多幻想,一家人在一起,想想...
    天天向上8287阅读 2,649评论 0 0