开发react也有一段时间了,一开始的随手写,生命周期乱用,无状态组件的不熟悉。现在逐渐规范一下,从网上各个地方copy过来,整理出一份文档。可能不全,后续还得多提炼总结和完善。

一、组件内方法书写,先写生命周期函数(按执行顺序写),再写自定义函数。

import React,{ Component } from 'react';

class Demo extends Component {
constructor(props,context) {
super(props,context)
this.state = {
//定义state
}
}
componentWillMount () {
}
componentDidMount () {
}
componentWillReceiveProps (nextProps) {
}
shouldComponentUpdate (nextProps,nextState) {
}
componentWillUpdate (nextProps,nextState) {
}
componentDidUpdate (prevProps,prevState) {
}
componentWillUnmount () {
} yourMethoed1(){
} yourMethoed2(){
}
render () {
return (
<div></div>
)
} }
export default Demo;

二、事件this绑定放到constrcutor构造函数中

import React,{ Component } from 'react';

class Demo extends Component {
constructor(props,context) {
super(props,context)
this.state = {
//定义state
}
this.handlerMethod = this.handlerMethod.bind(this)
} render () {
return (
<div></div>
)
}
}
export default Demo; // 不推荐写法:
<div onClick={this.handlerMethod.bind(this)}>do some actions</div>

三、组件一定要有prop传入类型校验,即要写PropTypes

注意:prop-types是第三方的npm包。react16版本后,自己不再维护PropTypes。因此要引用第三方的

import React,{Component} from 'react';
import PropTypes from 'prop-types';
class App extends Component{
render(){
return <div>{this.props.name}</div>
}
}
App. propTypes = {
name: PropTypes.string
}

四、异步获取数据请求放到componentDidMount中

五、尽量不要在钩子函数外使用setState方法,以及setTimeout中,不要在componentWillUpdate/componentDidUpdate/render中执行setState, 可能异致死循环。

六、访问真实dom方式:refs

<AudioCompoent  ref={(ref) => { this.myRef = ref; }}/>

// 不推荐
<AudioCompoent ref="myRef"/>

七、render方法内尽量少申明变量

八、数据遍历组件的时候要有key属性,但是不要用数组下标作为key

render() {
return (
<ul>
{this.state.sort.map((item, index) => {
return <li key={item.name}>
{item.name} <input type=“text”/>// 假定name是唯一的
</li> })}
</ul> );
}

九、简单展示类型,不涉及到state的组件,用function 函数声明式的无状态组件。

import React, {PropTypes} from 'react'
import {connect} from 'react-redux' const dataType = {
onExpand: PropTypes.func.isRequired,
isOpen: PropTypes.bool
} const List = ({ onExpand, expanded = false, childred }) =>
<form style={ expanded ? { height: 'auto' } : { height: } }>
{children}
<button onClick={onExpand}>Expand</button>
</form>; List.propTypes = dataType export default connect(List)









react组件开发规范总结的更多相关文章

  1. react组件开发规范(一)

    这是通过修改项目运行在Google上时的警告,总结的的部分react组件开发规范: (1)编写组件时,一定要写PropTypes,切莫为了省事儿而不写! 如果一个Props不是required,一定在 ...

  2. React组件开发入门

    React 组件开发入门 Introduction 本文组成: Ryan Clark文章Getting started with React的翻译. 博主的实践心得. React由Facebook的程 ...

  3. React组件开发(一)初识React

    *React不属于MVC.MVVM,只是单纯的V层. *React核心是组件(提高代码复用率.降低测试难度.代码复杂度). *自动dom操作,状态对应内容. *React核心js文件:react.js ...

  4. wn-cli 像React组件开发一样来开发微信小程序

    项目地址:wn-cli wn-cli wn-cli 像React组件开发一样来开发微信小程序 名字由来:wn -> weapp native 取第一个字母 Install npm install ...

  5. jquery插件模式开发和react组件开发之间的异同

    jquery插件模式开发和react组件开发之间的异同

  6. react复习总结(1)--react组件开发基础

    这次是年后第一次发文章,也有很长一段时间没有写文章了.准备继续写.总结是必须的. 最近一直在业余时间学习和复习前端相关知识点,在一个公司呆久了,使用的技术不更新,未来真的没有什么前景,特别是我们这种以 ...

  7. amazeui学习笔记二(进阶开发5)--Web 组件开发规范Rules

    amazeui学习笔记二(进阶开发5)--Web 组件开发规范Rules 一.总结 1.见名知意:见那些class名字知意,见函数名知意,见文件名知意 例如(HISTORY.md Web 组件更新历史 ...

  8. React代码开发规范

    前言 一般在团队开发中每个人的代码习惯都不太一样,这样就会导致代码风格不一致,以致于维护和修改bug的时候看别人的代码成为一种痛苦...这种情况尤其在前端开发中尤为明显.因为关于前端的开发规范貌似也没 ...

  9. React 组件开发初探

    react.js 在线地址:http://slides.com/yueyao/deck/#/ COMPONENT JSX 预编译语言, 一个基于ECMAscript 的xml-link 的语法扩展,最 ...

随机推荐

  1. JSP学习1---创建一个简单的jsp程序

    一.新建一个“Dynamic Web Project”动态Web项目 1.1输入项目名称 Project1,在Dynamic Web module version(动态Web模块版本),选择3.0(注 ...

  2. tensorboard OSError:[Errno 22] Invalid argument

    哈哈 问题解决了.感谢大佬 Bill.Z 附上原文链接:https://blog.csdn.net/u013244846/article/details/88380860 解决方法:更改manager ...

  3. django部署笔记

    在开发机上的准备工作: 确认项目没有bug. 用pip freeze > requirements.txt将当前环境的包导出到requirements.txt文件中,方便在部署的时候安装. 将项 ...

  4. 机器学习实战ch04 关于python版本所支持的文本格式问题

    函数定义中: def spamTest(): docList=[]; classList = []; fullText =[] for i in range(1,26):# print('cycle ...

  5. mplayer用法收集【转】

    转自:https://blog.csdn.net/wylhistory/article/details/4816653 1,录音: mplayer mms://202.***.***.***/test ...

  6. hive 的map数和reduce如何确定(转)

    转自博客:https://blog.csdn.net/u013385925/article/details/78245011(没找到原创者,该博客也是转发)   一.    控制hive任务中的map ...

  7. elasticSearch 2.3 delete-by-query plugin

    The delete-by-query plugin adds support for deleteing all of the documents which match the specified ...

  8. C# 6.0:nameof操作符

    C# 6.0 引入了一个名为“nameof”的新的操作符,它的作用是接收元素而后返回元素名字.这个操作符能将class和class的所用成员,比如方法.变量以及属性作为参数而后返回一个它们的名字.这避 ...

  9. nginx1.14.0版本https加密配置

    修改host文件,为最后访问域名准备 C:\Windows\System32\drivers\etc host文件目录192.168.10.140 www.joyce.com 在最后添加这个自定义域名 ...

  10. vscode之常用快捷键

    原文章地址: vscode: Visual Studio Code 常用快捷键 官方快捷键说明:Key Bindings for Visual Studio Code 主命令框 F1 或 Ctrl+S ...