初识React Native虚拟DOM节点及API
生活随笔
收集整理的這篇文章主要介紹了
初识React Native虚拟DOM节点及API
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
Reative Native是著名設社交網絡公司Facebook旗下的一款旨在實現虛擬Virtual交互到Native交互層高效交互的View層框架,通過虛擬DOM UI來轉為Native UI,通過JSX實現Javascript,IOS,Android原生開發語言的交互,充當一種橋梁(Bridge)的角色,類似一種交叉編譯。
雖然不能跨平臺應用,但能在各平臺上實現各自的代碼,但統一的風格,高效的開發受到眾多跨平臺。
學習網站:
React Native 官方文檔中文版
React Native
React Native Github地址
ReactJs官網
示例代碼
先來看看最簡單的代碼
<!DOCTYPE?html><html><head><meta?charset="UTF-8"?/><title>Hello?React!</title><!------------start:開發中必須使用的3個js庫---------------><script?src="../build/react.js"></script><script?src="../build/react-dom.js"></script><script?src="../build/browser.min.js"></script><!------------end:開發中必須使用的3個js庫---------------></head><body><div?id="example"></div><!--使用特定的script?type--><script?type="text/babel">//首先定義一個UI組建類 var?Hello?=?React.createClass({render:?function()?{//css樣式使用駝峰寫法,如classNamereturn?<div?className="commentBox">Hello?{this.props.name}{this.props.myAge}</div>;} }); <!--渲染虛擬節點,并且將虛擬節點添加到指定div,注意,這里沒有標準屬性,一切屬性屬于自定義--> ReactDOM.render(<Hello?name="World"?myAge="20"?/>,document.getElementById('container') );</script></body></html>當然,我們也可以使得代碼和UI分離
html
<!DOCTYPE?html> <html> <head><meta?charset="UTF-8"><title>Document</title><script?src="../build/react.js"></script><script?src="../build/react-dom.js"></script><script?src="../build/browser.min.js"></script><script?src="../build/jquery.min.js"></script><!--load?appjsx到頁面,注意,默認情況下不會load,除非改html放到服務器下--><script?type="text/babel"?src=js/app.js></script> </head> <body> <div?id="example"> </div> </body> </html>app.js---注意,舊版本是app.jsx
class?RepoList?extends?React.Component{//?<!--啟用構造函數,傳入參數-->constructor(props)?{super(props);this.state=?{loading:?true,error:?null,data:?null}}//?<!--當jsx渲染完成后執行-->componentDidMount()?{this.props.promise.then(value?=>?this.setState({loading:?false,?data:?value}),error?=>?this.setState({loading:?false,?error:?error}));}//?<!--當jsx渲染完成后執行-->render()?{if?(this.state.loading)?{return?<span>Loading...</span>;}else?if?(this.state.error?!==?null)?{return?<span>Error:?{this.state.error.message}</span>;}else?{var?repos?=?this.state.data.items;var?repoList?=?repos.map(function?(repo)?{return?(<li><a?href={repo.html_url}>{repo.name}</a>?({repo.stargazers_count}?stars)?<br/>?{repo.description}</li>);});return?(<main><h1>Most?Popular?JavaScript?Projects?in?Github</h1><ol>{repoList}</ol></main>);}} } //獲取js輸入,并賦值給promise,注意數據結構是?promise={{....}} ReactDOM.render(<RepoList?promise={$.getJSON('https://api.github.com/search/repositories?q=javascript&sort=stars')}?/>,document.body );轉載于:https://my.oschina.net/ososchina/blog/539802
總結
以上是生活随笔為你收集整理的初识React Native虚拟DOM节点及API的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu Terminal Shor
- 下一篇: RPM方式安装MySQL5.6和wind