React Native 教程 demo1 认识js与app原生控件

前端之家收集整理的这篇文章主要介绍了React Native 教程 demo1 认识js与app原生控件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

android ios效果图如下,android 使用F2或者摇一摇 ios使用⌘+R 来重新Reload JS

注意上面并不是webview 而是原生控件,请看片尾介绍



index.android.js和index.ios.js是一样的js


代码及解释如下,关于如何安装环境,和如何运行自行去官网一下。http://facebook.github.io/react-native/

如果React不熟悉,也建议先看去官网看一下,几个小时就够用了。http://www.reactjs.com

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

//声明api
var API_KEY = '7waqfqbprs7pajbz28mqf6vz';
var API_URL = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json';
var PAGE_SIZE = 25;
var PARAMS = '?apikey=' + API_KEY + '&page_limit=' + PAGE_SIZE;
var REQUEST_URL = API_URL + PARAMS;

var React = require('react-native');

//requires组件
var {
  AppRegistry,Image,StyleSheet,Text,View,ListView,} = React;

//定义组件
var AwesomeProject = React.createClass({
  //初始化状态的数据,loaded是否加载成功
  getInitialState: function() {
    return {
      dataSource: new ListView.DataSource({
        rowHasChanged: (row1,row2) => row1 !== row2,}),loaded: false,};
  },//React会在react-native组件加载完成后,使用componentDidMount方法发送请求,并且只发送一次。
  componentDidMount: function() {
    this.fetchData();
  },//加载网络数据
  fetchData: function() {
    fetch(REQUEST_URL)
      .then((response) => response.json())
      .then((responseData) => {
        this.setState({//在React的工作流程中,setState会触发一次重绘
          dataSource: this.state.dataSource.cloneWithRows(responseData.movies),loaded:true,});
      })
      .done();
  },//渲染组件
  render: function() {
    //
    if (!this.state.loaded) {
      return this.renderLoadingView();
    }
    return this.renderListView();
  },//渲染loading
  renderLoadingView:function() {
    return (
      <View style = {styles.container}>
        <Text>Loading movies</Text>
      </View>
      );
  },//渲染movie
  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>
      );
  },//渲染ListView
  renderListView:function(){
    return (
      <ListView
        dataSource={this.state.dataSource}
        renderRow={this.renderMovie}
        style={styles.listView} />
      );
  }

});

//定义样式表 FlexBox 官网https://css-tricks.com/snippets/css/a-guide-to-flexBox/
var styles = StyleSheet.create({
   container: {
    flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',flexDirection: 'row',marginBottom:10,marginLeft:10,marginRight:10,},thumbnail: {
    width: 53,height: 81,rightContainer: {
    flex: 1,title: {
    fontSize: 16,marginBottom: 4,textAlign: 'center',year: {
    fontSize: 12,listView: {
    paddingTop: 20,});

AppRegistry.registerComponent('AwesomeProject',() => AwesomeProject);
</pre><pre name="code" class="javascript">

上门写的js转换为原生app控件

原文链接:https://www.f2er.com/react/307634.html

猜你在找的React相关文章