ReactNative基礎教程

jopen 8年前發布 | 12K 次閱讀 Android開發 移動開發

通過tutorial文檔, 我們開發一個小的工程(ListView), 在Android和iOS使用相同的代碼, 展示出一樣的風格, 表明ReactNative開發的便捷.

顯示效果
效果

工欲善其事, 必先利其器. 開發環境Atom集成Nuclide.

Atom的配置和插件存儲在.atom文件夾下, 如果想重新使用Atom, 刪除文件夾即可. 在開發過程中, 使用Atom集成Nuclide會特別卡, 原因不明, 非死book也沒有給出解決答案. 也可以**暫時不安裝**Nuclide. 或使用PyCharm編寫.

1. React

配置apm命令, 打開Atom編輯器, Atom -> Install Shell Commands, 即可.
react-demos包含非死book提供的一些React的示例, react文檔提供了使用方法.

安裝顯示HTML控件Atom HTML Preview, apm install atom-html-preview.

啟動demo01的index.xml, atom index.html, 在頁面點擊ctrl+shift+h, 就可以看到HTML頁面.

HTML Preview

根據demo就可以學習React的內容, 主要是基于build中的組件, 開發簡單HTML頁面.

2. Tutorial

根據tutorial開發出展示ListView的應用, 可以運行于Android和iOS平臺之下, 不需要修改代碼, 在index.android.jsindex.ios.js下使用相同代碼即可.

具體內容可以參考文檔, 寫的已經很詳細了.

代碼

/**
 * Sample React Native App
 * https://github.com/非死book/react-native
 */
'use strict';

var React = require('react-native');
var {
  AppRegistry,
  Image,
  StyleSheet,
  Text,
  View,
  ListView,
} = React;

var MOCKED_MOVIES_DATA = [
  {title: 'Title', year: '2015', posters: {thumbnail: 'http://i.imgur.com/UePbdph.jpg'}},
];

var REQUEST_URL = 'https://raw.githubusercontent.com/非死book/react-native/master/docs/MoviesExample.json';

var AwesomeProject = React.createClass({
  componentDidMount: function() {
    this.fetchData();
  },

  getInitialState: function() {
    return {
      dataSource: new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
      }),
      loaded: false,
    };
  },

  fetchData: function() {
    fetch(REQUEST_URL)
      .then((response) => response.json()) .then((responseData) => { this.setState({ dataSource: this.state.dataSource.cloneWithRows(responseData.movies), loaded: true, }); }) .done(); }, render: function() { if (!this.state.loaded) { return this.renderLoadingView(); } return ( <ListView dataSource={this.state.dataSource} renderRow={this.renderMovie} style={styles.listView} /> ); }, renderLoadingView: function() { return ( <View style={styles.container}> <Text> Loading movies... </Text> </View> ); }, renderMovie: function(movie) { return ( <View style={styles.container}> <Image source={{uri: movie.posters.thumbnail}} style={styles.thumbnail} /> <View style={styles.rightContainer}> <Text style={styles.title}>{movie.title}</Text> <Text style={styles.year}>{movie.year}</Text> </View> </View> ); }, }); var styles = StyleSheet.create({ container: { flex: 1, flexDirection: 'row', justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, thumbnail: { width: 53, height: 81, }, rightContainer: { flex: 1, }, title: { fontSize: 20, marginBottom: 8, textAlign: 'center', }, year: { textAlign: 'center', }, listView: { paddingTop: 20, backgroundColor: '#F5FCFF', }, }); AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);

tutorial的代碼相同. render: function()是應用的主方法, JS代碼風格很清爽.

代碼的功能主要是從網上下載JSON數據, 把電影的名稱\年代\圖片, 通過stylesview的配合, 以列表的形式展現出來. 依賴var React = require('react-native');模塊, 使用ReactNative提供的功能開發JS邏輯, 在不同平臺展示.

git: https://github.com/SpikeKing/ReactNativeTutorial.git

OK, 基本開發已經完成了.

來自: http://blog.csdn.net//caroline_wendy/article/details/49721625

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!