React Native TypeScript 初次嘗試

TypeScript是JavaScript類型的超集谦屑,他可以編譯成純JavaScript驳糯。TypeScript可以在任何瀏覽器、任何計(jì)算機(jī)和任何操作系統(tǒng)上運(yùn)行氢橙,并且是開源的酝枢。

配置

1、官方文檔已經(jīng)寫的很詳細(xì)了悍手。參考: Using TypeScript with React Native
2帘睦、創(chuàng)建項(xiàng)目

react-native init MyAwesomeProject
cd MyAwesomeProject

3、添加TypeScript

yarn add --dev typescript
yarn add --dev react-native-typescript-transformer
yarn tsc --init --pretty --jsx react
touch rn-cli.config.js
yarn add --dev @types/react @types/react-native

4坦康、取消tsconfig.json中的一句注釋

{
  /* Search the config file for the following line and uncomment it. */
  // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
}

5竣付、在rn-cli.config.js寫入以下代碼

module.exports = {
  getTransformModulePath() {
    return require.resolve('react-native-typescript-transformer');
  },
  getSourceExts() {
    return ['ts', 'tsx'];
  },
};

嘗試用TypeScript做一個(gè)簡(jiǎn)單的聊天列表

1、效果圖


571535384093_.pic.jpg

2滞欠、目錄結(jié)構(gòu)


目錄結(jié)構(gòu)

3古胆、App.tsx

import React from 'react'
import { Component } from 'react';
import {
  ChatList
} from './src'

type Props = {};
type State = {};
export default class App extends Component<Props, State> {
  render() {
    return (
      <ChatList />
    );
  }
}

4、src/chat_list.tsx

import React from 'react'
import { Component } from 'react'
import {
    ScrollView,
    Platform
} from 'react-native'
import ChatItem from './chat_item'

interface State {}
interface Props {}

interface Item {
    img: Object,
    name: string,
    time: string,
    conetnt: string
}

const DATAS: Item[] = [
    {
        img: require('../assets/1.jpeg'),
        name: '高春',
        time: '10:43',
        conetnt: '你好啊,你好啊'
    },
    {
        img: require('../assets/2.jpeg'),
        name: '小花',
        time: '10:44',
        conetnt: '你好壞,你好啊'
    },
    {
        img: require('../assets/3.jpeg'),
        name: '小明',
        time: '12:43',
        conetnt: '你好傻,fdf,你好啊'
    },
    {
        img: require('../assets/1.jpeg'),
        name: '小李',
        time: '11:43',
        conetnt: '你好萌'
    },
    {
        img: require('../assets/2.jpeg'),
        name: '小清',
        time: '09:43',
        conetnt: '你好2'
    },
    {
        img: require('../assets/2.jpeg'),
        name: '小清',
        time: '09:43',
        conetnt: '你好2'
    },
    {
        img: require('../assets/2.jpeg'),
        name: '小清',
        time: '09:43',
        conetnt: '你好2'
    },
    {
        img: require('../assets/2.jpeg'),
        name: '小清',
        time: '09:43',
        conetnt: '你好2'
    },
    {
        img: require('../assets/2.jpeg'),
        name: '小清',
        time: '09:43',
        conetnt: '你好2'
    },
    {
        img: require('../assets/2.jpeg'),
        name: '小清',
        time: '09:43',
        conetnt: '你好2'
    },
    {
        img: require('../assets/2.jpeg'),
        name: '小清',
        time: '09:43',
        conetnt: '你好2'
    },
    {
        img: require('../assets/2.jpeg'),
        name: '小清',
        time: '09:43',
        conetnt: '你好2你好2你好2你好2'
    },
]

class ChatList extends Component<Props, State> {
    constructor (props: Props) {
        super(props);
    }
    render () {
        return (
            <ScrollView style={{marginTop: Platform.OS === 'ios' ? 24 : 0}}>
                { DATAS.map((item, index) => {
                    return <ChatItem item={item} key={index} />
                }) }
            </ScrollView>
        )
    }
}

export default ChatList

5筛璧、src/chat_item.tsx

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

interface Item {
    img: any,
    name: string,
    time: string,
    conetnt: string
}
interface State {}
interface Props {
    item: Item
}

class ChatItem extends Component<Props, State> {
    render () {
        const { item } = this.props
        return (
            <View>
                <View style={styles.container}>
                    <Image source={item.img} style={styles.headerImg} />
                    <View style={styles.contentView}>
                        <View style={styles.topView}>
                            <Text style={styles.titleText}>{item.name}</Text>
                            <Text style={styles.timeText}>{item.time}</Text>
                        </View>
                        <Text style={styles.contentText}>{item.conetnt}</Text>
                    </View>
                </View>
                <View style={styles.spliteLine} />
            </View>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        backgroundColor: 'white',
        padding: 15,
        flexDirection: 'row'
    },
    headerImg: {
        height: 80,
        width: 80
    },
    titleText: {
        fontSize: 18,
        color: 'black',
        fontWeight: 'bold',
        flex: 1
    },
    contentView: {
        flex: 1,
        paddingLeft: 10
    },
    topView: {
        flex: 1,
        flexDirection: 'row',
        paddingTop: 3
    },
    timeText: {
        fontSize: 14,
        color: '#b2b2b2'
    },
    contentText: {
        paddingBottom: 3,
        color: '#b2b2b2',
        fontSize: 16
    },
    spliteLine: {
        borderTopWidth: 0.5,
        borderTopColor: '#b2b2b2'
    }
})

export default ChatItem

6逸绎、src/index.ts

import ChatList from './chat_list'
export {
    ChatList
}

總結(jié)

1妖滔、上手很容易
2、比JavaScript寫起來(lái)麻煩那么一丟丟
3桶良、總體來(lái)說還是不錯(cuò)的

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市沮翔,隨后出現(xiàn)的幾起案子陨帆,更是在濱河造成了極大的恐慌,老刑警劉巖采蚀,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疲牵,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡榆鼠,警方通過查閱死者的電腦和手機(jī)纲爸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)妆够,“玉大人识啦,你說我怎么就攤上這事∩衩茫” “怎么了颓哮?”我有些...
    開封第一講書人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)鸵荠。 經(jīng)常有香客問我冕茅,道長(zhǎng),這世上最難降的妖魔是什么蛹找? 我笑而不...
    開封第一講書人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任姨伤,我火速辦了婚禮,結(jié)果婚禮上庸疾,老公的妹妹穿的比我還像新娘乍楚。我一直安慰自己,他們只是感情好彼硫,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開白布炊豪。 她就那樣靜靜地躺著,像睡著了一般拧篮。 火紅的嫁衣襯著肌膚如雪词渤。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評(píng)論 1 305
  • 那天串绩,我揣著相機(jī)與錄音缺虐,去河邊找鬼。 笑死礁凡,一個(gè)胖子當(dāng)著我的面吹牛高氮,可吹牛的內(nèi)容都是我干的慧妄。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼剪芍,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼塞淹!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起罪裹,我...
    開封第一講書人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤饱普,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后状共,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體套耕,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年峡继,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了冯袍。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡碾牌,死狀恐怖康愤,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情舶吗,我是刑警寧澤翘瓮,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站裤翩,受9級(jí)特大地震影響资盅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜踊赠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一呵扛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧筐带,春花似錦今穿、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至帖鸦,卻和暖如春芝薇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背作儿。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工洛二, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓晾嘶,卻偏偏與公主長(zhǎng)得像妓雾,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子垒迂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

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