RN 學(xué)習(xí)之開始篇

安裝:

npm install -g react-native-cli

查看當(dāng)前版本:

react-native --version

我自己當(dāng)前的版本是:react-native-cli: 2.0.1,react-native: 0.51.0

創(chuàng)建:

react-native init FirstProject

運行 APP (iOS):

cd FirstProject
react-native run-ios

運行 APP (Android):

cd FirstProject
react-native run-android

創(chuàng)建目錄如下:

+ __tests__
+ ios
+ android
+ node_modules
- package-lock.json
- package.json
- app.json
- index.js
- App.js

寫上“Hello, world!”:
把App.js里面的代碼全部刪了,寫上下面的代碼:

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

export default class App extends Component {
  render() {
    return (
      <Text>Hello, world!</Text>
    );
  }
}

當(dāng)然你寫上這段代碼后悔看到的效果是——
“Hello, world!”,這段文字的位置在屏幕左上角頂部開始的。

調(diào)整一下代碼:

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

const instructions = Platform.select({
  ios: 'Hello, iOS!',
  android: 'Hello, Android!',
});

export default class App01 extends Component<{}> {
  render() {
    return (
            <View style={styles.container}>
                <Text> Hello, world! </Text>
                <Text> {instructions} </Text>
        </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#fff',
  }
});
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容