I Love ReactJS

It may be the 50 React + TypeScript specifications and experiences you need(1)

there is no distinction between right and wrong, and there must be some imperfections, combining my daily development and experience. It allows you to write more rigorous code, and I hope it will be helpful after reading it.

1. Comments

(1) comments at the top of the file, including description, author, date

/**
 * @description xxxxxx
 * @author chengfeng
 * @since 19/05/21
 */

(2) comments on the module

/**
 * Copy Data
 * @param  {*}  data   Source data to copy
 * @param  {boolean} [isDeep=false] Is it a deep copy? Default is a shallow copy
 * @return {*}         Return copied data
 */

(3) Business code comments

/*Business Code Comment*/

(4) variable comments

interface IState {
  // Name
  name: string;
  // Telephone
  phone: number;
  // Address
  address: string;
}

2. Reference component order

import * as React from 'react';
import { Dropdown, Menu, Icon } from 'antd';
import Header from './Header';
import toast from 'common/toast';
import './index.less';

3. Quotation marks

4. Indent

const handleCheck = () => {
  onCancel && onCancel();
  onClose && onClose();
};

5. Semicolon

6. Parentheses

the following keywords must be followed by curly braces (even if the content of the code block is only one line): if, else, for, while, do, switch, try, catch, finally, with.

// not good
if (condition) doSomething();

// good
if (condition) {
  doSomething();
}

7. Space

// bad
++ x;
y ++;
z = x?1:2;

// good
++x;
y++;
z = x ? 1 : 2;
// bad
if (condition){
}

while (condition){
}

function funcName(){
}

// good
if (condition) {
}

while (condition) {
}

function funcName() {
}

// bad
if(condition) {
}

while(condition) {
}

(function() {
})();

// good
if (condition) {
}

while (condition) {
}

(function () {
})();
// bad
var obj = {
    a : 1,
    b:2,
    c :3
};

// good
var obj = {
    a: 1,
    b: 2,
    c: 3
};

8. Line break

// bad
var obj = {
    a: 1
    , b: 2
    , c: 3,
};

function test()
{
    ...
}
for (const key in object)
 {
  if (object.hasOwnProperty(key)) {
    const element = object[key];

  }
}
// good
var obj = {
    a: 1,
    b: 2,
    c: 3,
};

function test() {
    ...
}

for (const key in object) {
  if (object.hasOwnProperty(key)) {
    const element = object[key];

  }
}
// bad
if (condition) {
    ...
}
else {
    ...
}

try {
    ...
}
catch (e) {
    ...
}
finally {
    ...
}


// good
if (condition) {
    ...
} else {
    ...
}

try {
    ...
} catch (e) {
    ...
} finally {
    ...
}

9. Array, object


// bad
const a = {
    'b': 1
};

const a = {b: 1};

const a = {
    b: 1,
    c: 2
};
const arr = [1, 2, 3, 4,];

// good
const a = {
    b: 1,
    c: 2,
};

const arr = [1, 2, 3, 4];

10. Name

interface IProps {
  onClose?: () => void;
  onOk?: (item: Record<string, any>) => void;
}

The event function within the

interface IProps {}
interface IState {}
Exit mobile version