React小记

1、React Component 一定要大写开头

在JSX中,小写标签名称被认为是HTML标签。但是,带小点(属性访问器)的小写标签名不是。

查看HTML标记与React组件

  • <component />编译为React.createElement('component')(html标签)
  • <Component /> 编译 React.createElement(Component)
  • <obj.component /> 编译 React.createElement(obj.component)

2、type

type someType = string;

上面的含义是:someType与string等价,string类型有了另一个名字,那么定义以后怎么用呢?

let a: someType = 'Tim';

 

3、便捷的枚举类型写法

const arr = ['a', 'b', 'c'] as const;

type ABC = typeof arr[number];

// 等价于
type ABC = 'a' | 'b' | 'c'

 

4、需要规定对象每个value都是同一个类型时

const keys = ['zhang', 'a', 'mie'] as const;

type keysType = typeof keys[number];
interface Item {
    name: string;
    price: number;
}

const itemMap: {[key in keysType]: Item} = {
    zhang: {
        name: 'aaaa',
        price: 123
    }
}

 

posted @ 2019-11-11 15:56  张啊咩  阅读(97)  评论(0编辑  收藏  举报