ReactJS中的自定義組件實例代碼
React 是一個用于構(gòu)建用戶界面的 JAVASCRIPT 庫。
React 主要用于構(gòu)建UI,很多人認為 React 是 MVC 中的 V(視圖)。
React 起源于 Facebook 的內(nèi)部項目,用來架設 Instagram 的網(wǎng)站,并于 2013 年 5 月開源。
React 擁有較高的性能,代碼邏輯非常簡單,越來越多的人已開始關(guān)注和使用它。
React 特點
1.聲明式設計 −React采用聲明范式,可以輕松描述應用。
2.高效 −React通過對DOM的模擬,最大限度地減少與DOM的交互。
3.靈活 −React可以與已知的庫或框架很好地配合。
4.JSX − JSX 是 JavaScript 語法的擴展。React 開發(fā)不一定使用 JSX ,但我們建議使用它。
5.組件 − 通過 React 構(gòu)建組件,使得代碼更加容易得到復用,能夠很好的應用在大項目的開發(fā)中。
6.單向響應的數(shù)據(jù)流 − React 實現(xiàn)了單向響應的數(shù)據(jù)流,從而減少了重復代碼,這也是它為什么比傳統(tǒng)數(shù)據(jù)綁定更簡單。
可控自定義組件:
<!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>
不可控的自定義組件:
<!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中的自定義組件實例代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關(guān)文章
react-router?v6實現(xiàn)權(quán)限管理+自動替換頁面標題的案例
這篇文章主要介紹了react-router?v6實現(xiàn)權(quán)限管理+自動替換頁面標題,這次項目是有三種權(quán)限,分別是用戶,商家以及管理員,這次寫的權(quán)限管理是高級權(quán)限能訪問低級權(quán)限的所有頁面,但是低級權(quán)限不能訪問高級權(quán)限的頁面,需要的朋友可以參考下2023-05-05使用react-native-doc-viewer實現(xiàn)文檔預覽
這篇文章主要介紹了使用react-native-doc-viewer實現(xiàn)文檔預覽,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09react-navigation之動態(tài)修改title的內(nèi)容
這篇文章主要介紹了react-navigation之動態(tài)修改title的內(nèi)容,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09React?Native中原生實現(xiàn)動態(tài)導入的示例詳解
在React?Native社區(qū)中,原生動態(tài)導入一直是期待已久的功能,在這篇文章中,我們將比較靜態(tài)和動態(tài)導入,學習如何原生地處理動態(tài)導入,以及有效實施的最佳實踐,希望對大家有所幫助2024-02-02