欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

了解index.ios.js

程序员文章站 2022-04-27 08:52:12
...

1、index.ios.js文件

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

export default class MNAPP extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          React-Native 入门学习
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
    color: 'red',
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('MNAPP', () => MNAPP);

(1)引入其他模块

import React, { Component } from 'react';

(2)批量定义组件

import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

(3)构建MNAPP入口类:render方法用于渲染视图

// JSX的模板语法
export default class MNAPP extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          React-Native 入门学习
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
      </View>
    );
  }
}

(4)JS的自面量表达了css样式

const styles = StyleSheet.create({
});
  • style是视图的一个属性
  • container、welcome、instructions:样式表中的样式

(5)注册应用入口

AppRegistry.registerComponent('MNAPP', () => MNAPP);

2、用Xcode打开AppDelegate.m

(1)标识入口文件

NSURL *jsCodeLocation;
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

(2)接下来尝试修改启动界面:LaunchScreen.xib

了解index.ios.js

(3)添加一张图片

  • 导入image
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Image // ①
} from 'react-native';
  • 添加样式
export default class MNAPP extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          React-Native 入门学习
        </Text>
        <Image style={styles.pic} source={{uri: 'https://avatars3.githubusercontent.com/u/6133685?v=3&s=460'}}>
        </Image> // ②
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
    color: 'red',
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
  pic: { // ③
     height: 100,
     width: 100,
  },
});

GitHub:https://github.com/Yangchengfeng/react-native-learning/tree/learnMoreOfIndexIosJS