react-components-docs/react编码规范.md

639 lines
12 KiB
Markdown
Raw Permalink Normal View History

2016-09-30 12:52:53 +08:00
# react组件开发规范
2016-10-08 14:15:57 +08:00
2016-12-19 08:25:59 +08:00
要求编码规范,接口定义规范,组件结构自由给予用户充分定制能力。
2016-10-08 14:15:57 +08:00
## 文件命名
- 每一个文件只包含一个组件,每一个基本组件只包含单一功能
- src目录下如果文件返回是一个类文件名首字母大写
- 文件js模块统一使用js后缀名
2017-12-07 14:34:53 +08:00
- React组件文件使用大驼峰命名方式其他文件一律使用小写命名如果需要使用中划线分割。
2016-10-08 14:15:57 +08:00
## js规范
2017-10-19 12:46:22 +08:00
- 使用es6开发尽量使用常用的ES6语法[ES6语法参考](http://es6.ruanyifeng.com/)
2016-09-30 12:52:53 +08:00
- 使用jsx语法
2017-10-17 09:50:53 +08:00
### 格式
- 标签自闭合
```
//bad
<Button></Button>
//good
<Button />
```
- 自闭合换行
```
//bad
<Button color="primary" />
//good
<Button
color="primary"
/>
```
-
### 组件命名
2016-10-17 15:55:35 +08:00
- 组件文件命名使用大驼峰, ComponentDemo
2016-09-30 12:52:53 +08:00
- 带命名空间的组件如果一个组件包含只有自身使用的子组件以该组件为命名空间编写组件例如TableTable.Head
2017-10-17 09:50:53 +08:00
### 组件声明
- 使用es6的class来声明组件而不是使用`createClass`,在React V16版本`createClass`已经废弃
```
// bad
const Button = React.createClass({
// ...
render() {
return <button>{this.state.hello}</button>;
}
});
// good
class Button extends React.Component {
// ...
render() {
return <button>{this.state.hello}</button>;
}
```
- 对于不包含`state`和`refs`的组件建议写成纯函数组件而且建议使用function关键字声明
```
//bad
class Button extends React.Component{
render() {
return (
<Button>{ this.props.text }</Button>
)
}
}
//bad
const Button = ({ text }) => (
<button>{ text }</button>
)
//good
function Button ({ text }) {
return (
<button>{ text }</button>
)
}
```
- 对于只有简单数据类型的`props`和`state`的组件,建议使用`PureComponent`来声明组件
```
//bad
class Button extends React.Component{
constructor(props){
super(props);
this.state = {
disabled: false
}
}
render() {
return (
<Button
disabled={this.state.disabled}>
{ this.props.text }
</Button>
)
}
}
//good
class Button extends React.PureComponent{
constructor(props){
super(props);
this.state = {
disabled: false
}
}
render() {
return (
<Button
disabled={this.state.disabled}>
{ this.props.text }
</Button>
)
}
}
```
### 组件内部声明
2016-09-30 12:52:53 +08:00
- 使用propTypes进行props类型校验
2017-10-17 09:50:53 +08:00
```
import PropTypes from 'prop-types';
const propTypes = {
disabled: PropTypes.bool
}
class Button extends React.Component{}
Button.propTypes = propTypes;
```
2016-09-30 12:52:53 +08:00
- 使用defaultProps定义默认参数
2017-10-17 09:50:53 +08:00
```
import PropTypes from 'prop-types';
const defaultProps = {
disabled: false
}
class Button extends React.Component{}
Button.defaultProps = defaultProps;
```
- 不使用displayName命名
displayName属性用于组件调试时输出显示JSX自动设置该值可以理解为调试时显示的组件名。
```
// bad
export default React.createClass({
displayName: 'Button',
// stuff goes here
});
// good
export default class Button extends React.Component {
}
```
### 属性定义
2016-10-14 15:38:12 +08:00
- 定义props避开react关键字及保留字常用的props及state定义可参考下表
2017-10-17 09:50:53 +08:00
- `props`和`state`使用驼峰命名。
- 不直接修改`state`
```
//bad
class Button extends React.Component{
constructor(props){
super(props);
this.state = {
disabled: false
}
}
handleClick = () => {
this.state.disabled = true;
this.setState({
disabled: this.state.disabled
})
}
render() {
return (
<Button
disabled={this.state.disabled}>
{ this.props.text }
</Button>
)
}
}
//good
class Button extends React.Component{
constructor(props){
super(props);
this.state = {
disabled: false
}
}
handleClick = () => {
let disabled = ture;
this.setState({
disabled
})
}
render() {
return (
<Button
disabled={this.state.disabled}>
{ this.props.text }
</Button>
)
}
}
```
- 自定义HTML属性使用data-
```
class Button extends React.Component{
render() {
return (
<Button
data-name="submit-button">
{ this.props.text }
</Button>
)
}
}
```
- 可以使用es6的这种`...props`语法获取剩下所有属性,但是如果没有必要,尽量不要使用
```
class Button extends React.Component{
render() {
let {onClick, disabled, ...props} = this.props;
return (
<Button
{ ...props }>
{ this.props.text }
</Button>
)
}
}
```
### ref
- 通过`ref`可以取到组件原生dom对象。使用传入函数function方式来定义`ref`,不使用字符串定义`ref`
```
//bad
class Button extends React.Component{
constructor(props){
super(props);
}
componentDidMount() {
this.refs.buttonRef.focus();
}
render() {
return (
<Button
ref="buttonRef">
{ this.props.text }
</Button>
)
}
}
//good
class Button extends React.Component{
constructor(props){
super(props);
this.buttonRef = {};
}
componentDidMount() {
this.buttonRef.focus();
}
render() {
return (
<Button
ref={(el) => this.buttonRef = el;}>
{ this.props.text }
</Button>
)
}
}
```
2016-09-30 12:52:53 +08:00
- 尽量少或者不使用ref获取和操作dom节点使用state和prop进行控制dom
2017-10-17 09:50:53 +08:00
### 组件声明周期函数
- 组件方法定义顺序
- constructor
- getChildContext
- componentWillMount
- componentDidMount
- componentWillReceiveProps
- shouldComponentUpdate
- componentWillUpdate
- componentDidUpdate
- componentWillUnmount
- 在`componentDidMount`生命周期内获取初始数据
```
class Button extends React.Component{
constructor(props){
super(props);
this.state = {
data: ''
}
}
componentDidMount() {
axios.get('http://baidu.com/button-name')
.then((res) => {
this.setState({
datares.data
})
})
}
render() {
return (
<Button>
{ this.state.data }
</Button>
)
}
}
```
- 在组件`componentWillUnmount`卸载生命周期中,进行清除定时器、卸载组件绑定的自定义事件等。
```
class Button extends React.Component{
constructor(props){
super(props);
this.state = {
data: ''
}
this.timer = null;
}
componentDidMount() {
this.timer = setInterval(this.getData, 1000);
}
getData = () => {
axios.get('http://baidu.com/button-name')
.then((res) => {
this.setState({
datares.data
})
})
}
componentWillUnmount() {
window.clearInterval(this.timer);
}
render() {
return (
<Button>
{ this.state.data }
</Button>
)
}
}
```
- 使用箭头函数定义自定义的组件内方法
```
//bad
class Button extends React.Component{
handleClick(e) {
}
render() {
return (
<Button onClick={this.handleClick.bind(this)}>
{ this.state.data }
</Button>
)
}
}
//good
class Button extends React.Component{
handleClick = (e) => {
}
render() {
return (
<Button onClick={this.handleClick}>
{ this.state.data }
</Button>
)
}
}
```
### Mixin
- 使用es6后不支持mixin使用decorator进行扩展和高阶组件方式扩展。
2016-10-14 15:40:57 +08:00
2016-10-08 14:15:57 +08:00
2016-09-30 12:52:53 +08:00
- 代码规范使用 [airbnb规范](https://github.com/airbnb/javascript/tree/master/react)
2016-09-30 13:49:30 +08:00
2016-10-08 14:15:57 +08:00
## 样式规范
2016-10-10 16:45:37 +08:00
- 组件样式使用sass编写公用样式使用tinper-bee-core包请阅读[tinper-bee-core文档](https://github.com/tinper-bee/tinper-bee-core)
2016-10-08 14:15:57 +08:00
- 组件样式调用使用classnames模块进行样式处理使用className调用
2016-11-24 14:09:25 +08:00
- 所有组件默认类名命名以`u-``开头
- 组件使用clsPrefix为样式前缀,用户也可在组件上设置自定义的`clsPrefix="yourPre"`
2016-10-08 14:15:57 +08:00
```javascript
const clsPrefix = 'u-select';
const class1 = {
[`${clsPrefix}-item`]: true,
[`${clsPrefix}-item-last`]: stepLast,
[`${clsPrefix}-status-${status}`]: true,
2016-10-17 15:14:49 +08:00
[`${clsPrefix}-custom`]: icon
2016-10-08 14:15:57 +08:00
};
const class2 = [`${clsPrefix}-submit`, `${clsPrefix}-item`];
const classString = classNames('hide', class1, class2);
```
2016-11-24 14:05:10 +08:00
- 可提供多种颜色的组件在写scss文件时颜色设置需提出公用的minxin方法例如Alert组件中设置多种颜色 @alert-styles-variant
2016-11-24 14:09:25 +08:00
## 通用组件接口规范
2016-10-12 09:56:00 +08:00
|参数|说明|类型|默认值|
|---|----|---|------|
|size|尺寸|string|medium|
|color|颜色|string|''|
|shape|形状|string|''|
|disabled|是否禁用(`disabled` 或 `true` `false`)|bool|false|
|className|增加额外的类名|string|''|
|htmlType|html dom 的 type 属性|string|''|
|style|内联样式|object|''|
2016-11-24 14:09:25 +08:00
|clsPrefix|自定义样式前缀|string|''|
2016-10-08 14:15:57 +08:00
2016-12-19 08:25:59 +08:00
对于方法的传递外部使用onClick传入事件内部使用handleClick进行接收使用
2016-12-04 18:47:31 +08:00
## 国际化
当你的组件包含一些文字时在src目录下创建`i18n.js`文件写下对应的hash值。内容如下
```
module.exports = {
'zh-cn': {
'ok': '确定',
'cancel': '取消',
'isee': '知道了'
},
'en-us': {
'ok': 'ok',
'cancel': 'cancel',
'isee': 'ok'
}
}
```
组件内这么使用:
```
import i18n from './i18n';
import React from 'react';
Example.defaultProps = {
locale: 'zh-cn'
};
class Example extends React.Component {
constructor(props) {
super(props);
}
render() {
const {locale} = this.props;
const locale = i18n[locale];
const buttons= [
<Button>
{locale['ok']}
</Button>,
<Button>
{locale['cancel']}
</Button>
];
return (
<div>
{ buttons }
</div>
)
}
}
```
2016-09-30 13:49:30 +08:00
## 基本代码结构
```javascript
//引入依赖
2016-10-11 13:30:13 +08:00
import React from 'react';
import ReactDOM from'react-dom';
import classnames from 'classnames';
2016-09-30 13:49:30 +08:00
//定义prop检验
const propTypes = {
2016-10-14 15:38:12 +08:00
//每一个props都要写注释
2016-09-30 13:49:30 +08:00
}
//定义默认参数
const defaultProps = {
}
2016-10-14 15:40:57 +08:00
/**
* 定义组件
*/
2016-09-30 13:49:30 +08:00
class Button extends React.Component {
constructor (props) {
super(props);
//定义state
this.state = {
2016-10-14 15:38:12 +08:00
//每一个state要写注释
2016-09-30 13:49:30 +08:00
}
2016-10-14 15:40:57 +08:00
// 事先声明方法绑定
this.MyEvent = this.MyEvent.bind(this);
2016-09-30 13:49:30 +08:00
}
2016-10-14 15:38:12 +08:00
//自定义函数方法,及注释
2016-10-17 15:27:47 +08:00
MyEvent () {
}
2016-10-14 15:38:12 +08:00
//组件生命周期方法
2016-09-30 13:49:30 +08:00
2016-10-17 15:27:47 +08:00
2016-09-30 13:49:30 +08:00
render () {
return (
2016-10-14 15:40:57 +08:00
// <div onClick={this.MyEvent}></div>
2016-09-30 13:49:30 +08:00
)
}
}
Button.propTypes = propTypes;
Button.defaultProps = defaultProps;
2016-10-11 13:30:13 +08:00
export default Button;
2016-09-30 13:49:30 +08:00
```
2016-10-08 14:15:57 +08:00
2017-05-22 10:34:14 +08:00
### 常用npm包
2017-08-22 14:41:06 +08:00
##### [keyCode](https://github.com/timoxley/keycode)
2017-05-22 10:34:14 +08:00
2017-08-22 14:41:06 +08:00
##### [warning](https://github.com/BerkeleyTrue/warning)
2017-05-22 10:34:14 +08:00
2017-08-22 14:41:06 +08:00
```
var warning = require('warning');
var ShouldBeTrue = false;
warning(
ShouldBeTrue,
'This thing should be true but you set to false. No soup for you!'
);
```
##### [bee-animate](https://github.com/tinper-bee/bee-animate)
2017-05-22 10:34:14 +08:00
2017-08-22 14:41:06 +08:00
##### [bee-overlay](https://github.com/tinper-bee/bee-overlay)
2017-05-22 10:34:14 +08:00
2017-08-22 14:41:06 +08:00
##### [dom-helpers (3.0.0)](https://github.com/react-bootstrap/dom-helpers)
2017-05-22 10:34:14 +08:00
2016-10-08 14:15:57 +08:00
参考链接
https://github.com/react-component/react-component.github.io/blob/master/docs/zh-cn/code-style/js.md