加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:46:58 所属栏目:百科 来源:网络整理
导读:android ios效果图如下,android 使用F2或者摇一摇 ios使用?+R 来重新Reload JS 注意上面并不是webview 而是原生控件,请看片尾介绍 index.android.js和index.ios.js是一样的js 代码及解释如下,关于如何安装环境,和如何运行自行去官网一下。http://faceboo

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控件

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读