some_thing_about_react

一、React Components & React Elements & Instance

components: 输入props,输出element Tree. 单一数据流。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 1、作为functions定义
const Button = ({children, color}) => ({
type: 'button',
props: {
className: 'button button-'+color,
children: {
type: 'b',
props: {
children: children
}
}
}
})

// 2、使用React.createClass()
const Button = React.createClass({
render(){
const { children, color } = this.props
return {
type: 'button',
props: {
className: 'button button-' + color,
children: {
type: 'b',
props: {
children:children
}
}
}
}
}
})

//3、使用ES6的class继承自React.Component
class Button extends React.Component{
render(){
const { children, color } = this.props
return {
type: 'button',
props: {
className: 'button button-' + color
children: {
type: 'b',
props: {
children: children
}
}
}
}
}
}
elements: 一个不可变的描述对象,告诉react要绘制什么样的视图。element本质上只是普通对象,描述了component instance 和 DOM Node,包括这俩者想要得到的属性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
type: (string | ReactClass),
props: Object
$$typeof: Symbol.for('react.element')
}

i.e.
{
type: 'button',
props: {
className: 'button button-blue',
children: {
type: 'b',
props: {
children: 'ok'
}
}
}
}
得到以下结果:
<button className='button button-blue'>
<b>
ok
</b>
</button>

// 1、通过React.createElement创建React element
React.createElement(
type,
[props],
[...children]
)

// 2、JSX
const name = 'joeng'
const Ele = <div>hello,{name}</div>


// 校验是否是React Element
React.isValidElement(object)
instances: 只有用class声明的React Components才有instance,instance是React创建和管理的,比起components和elements来说,instance显得不是那么重要。
# react

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×