React 是一個(gè)用于構(gòu)建用戶界面的 JAVASCRIPT 庫(kù)。
React 主要用于構(gòu)建UI,很多人認(rèn)為 React 是 MVC 中的 V(視圖)。
React 起源于 Facebook 的內(nèi)部項(xiàng)目,用來架設(shè) Instagram 的網(wǎng)站,并于 2013 年 5 月開源。
React 擁有較高的性能,代碼邏輯非常簡(jiǎn)單,越來越多的人已開始關(guān)注和使用它。
React 特點(diǎn)
1.聲明式設(shè)計(jì) −React采用聲明范式,可以輕松描述應(yīng)用。
2.高效 −React通過對(duì)DOM的模擬,最大限度地減少與DOM的交互。
3.靈活 −React可以與已知的庫(kù)或框架很好地配合。
4.JSX − JSX 是 JavaScript 語法的擴(kuò)展。React 開發(fā)不一定使用 JSX ,但我們建議使用它。
5.組件 − 通過 React 構(gòu)建組件,使得代碼更加容易得到復(fù)用,能夠很好的應(yīng)用在大項(xiàng)目的開發(fā)中。
6.單向響應(yīng)的數(shù)據(jù)流 − React 實(shí)現(xiàn)了單向響應(yīng)的數(shù)據(jù)流,從而減少了重復(fù)代碼,這也是它為什么比傳統(tǒng)數(shù)據(jù)綁定更簡(jiǎn)單。
可控自定義組件:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
<!DOCTYPE html> <html> <head> <meta charset= "UTF-8" > <title></title> <script src= "js/react.js" ></script> <script src= "js/react-dom.js" ></script> <script src= "js/browser.min.js" ></script> </head> <body> <script type= "text/babel" > var Radio=React.createClass({ getInitialState: function (){ return { value: this .props.defaultValue }; }, handleChange: function (event){ if ( this .props.onChange){ this .props.onChange(event); } this .setState({ value:event.target.value }); }, render: function (){ var children=[]; var value= this .props.value|| this .state.value; React.Children.forEach( this .props.children, function (child,i){ var label=<label key={i}> <input type= "radio" name={ this .props.name} value={child.props.value} checked={child.props.value==value} onChange={ this .handleChange}/> {child.props.children} <br/> </label>; children.push(label); }.bind( this )); return <span>{children}</span>; } }); var MyForm=React.createClass({ getInitialState: function (){ return ({my_radio: "B" }); }, handleChange: function (event){ this .setState({ my_radio:event.target.value }); }, submitHandler: function (event){ event.preventDefault(); alert( this .state.my_radio); }, render: function (){ return ( <form onSubmit={ this .submitHandler}> <Radio value={ this .state.my_radio} name= "my_radio" onChange={ this .handleChange}> <option value= "A" >First option</option> <option value= "B" >Second option</option> <option value= "C" >Third option</option> </Radio> <button type= "submit" >Speak</button> </form> ) } }); ReactDOM.render(<MyForm></MyForm>,document.body); </script> </body> </html> |
不可控的自定義組件:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
<!DOCTYPE html> <html> <head> <meta charset= "UTF-8" > <title></title> <script src= "js/react.js" ></script> <script src= "js/react-dom.js" ></script> <script src= "js/browser.min.js" ></script> </head> <body> <script type= "text/babel" > var Radio=React.createClass({ getInitialState: function (){ return { value: this .props.defaultValue }; }, handleChange: function (event){ if ( this .props.onChange){ this .props.onChange(event); } this .setState({ value:event.target.value }); }, render: function (){ var children=[]; var value= this .props.value|| this .state.value; React.Children.forEach( this .props.children, function (child,i){ var label=<label> <input type= "radio" name={ this .props.name} value={child.props.value} checked={child.props.value==value} onChange={ this .handleChange}/> {child.props.children} <br/> </label>; children[ 'label' +i]=label; }.bind( this )); return <span>{children}</span>; } }); var MyForm=React.createClass({ handleSubmit: function (event){ event.preventDefault(); alert( this .refs.radio.state.value); }, render: function (){ return ( <form onSubmit={ this .handleSubmit}> <Radio ref= "radio" name= "my_radio" defaultValue= "B" > <p value= "A" >First</p> <option value= "B" >Second option</option> <option value= "C" >Third option</option> </Radio> <button type= "submit" >Speak</button> </form> ) } }); ReactDOM.render(<MyForm></MyForm>,document.body); </script> </body> </html> <!DOCTYPE html> <html> <head> <meta charset= "UTF-8" > <title></title> <script src= "js/react.js" ></script> <script src= "js/react-dom.js" ></script> <script src= "js/browser.min.js" ></script> </head> <body> <script type= "text/babel" > var Radio=React.createClass({ getInitialState: function (){ return { value: this .props.defaultValue }; }, handleChange: function (event){ if ( this .props.onChange){ this .props.onChange(event); } this .setState({ value:event.target.value }); }, render: function (){ var children=[]; var value= this .props.value|| this .state.value; React.Children.forEach( this .props.children, function (child,i){ var label=<label> <input type= "radio" name={ this .props.name} value={child.props.value} checked={child.props.value==value} onChange={ this .handleChange}/> {child.props.children} <br/> </label>; children[ 'label' +i]=label; }.bind( this )); return <span>{children}</span>; } }); var MyForm=React.createClass({ handleSubmit: function (event){ event.preventDefault(); alert( this .refs.radio.state.value); }, render: function (){ return ( <form onSubmit={ this .handleSubmit}> <Radio ref= "radio" name= "my_radio" defaultValue= "B" > <p value= "A" >First</p> <option value= "B" >Second option</option> <option value= "C" >Third option</option> </Radio> <button type= "submit" >Speak</button> </form> ) } }); ReactDOM.render(<MyForm></MyForm>,document.body); </script> </body> </html> |
總結(jié)
以上所述是小編給大家介紹的ReactJS中的自定義組件實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
原文鏈接:https://www.cnblogs.com/xsd1/archive/2019/11/23/11917746.html