react-native 項(xiàng)目的結(jié)構(gòu)

初始化一個(gè)項(xiàng)目

react-native init 項(xiàng)目名稱

react-native的項(xiàng)目的目錄結(jié)構(gòu):

src/
    pages/
        Home/
        Near/
        Order/
        Mine/
    components/
        自定義組件
    common/
        sreen
        color
    navigator/
        MainNavigator/
    store/
    network/

react-native 項(xiàng)目添加新的依賴庫(kù):

在終端界面進(jìn)入項(xiàng)目文件夾中,

npm install --save 依賴庫(kù)

例子:

npm install --save react-navigation

運(yùn)行之后會(huì)發(fā)現(xiàn)項(xiàng)目的 package.json的

"dependencies": {
    "react": "16.0.0",
    "react-native": "0.50.3",
    "react-navigation": "^1.0.0-beta.19"
  },

的依賴文件中多個(gè) "react-navigation": "^1.0.0-beta.19"

JSX 語(yǔ)法沒有前向聲明,組件的定義要在使用之前。

RN app 的組件的結(jié)構(gòu)。

棧導(dǎo)航作為根組件來(lái)管理組件的Push Pop

const MainNavigator = StackNavigator(
    // 配置路由
    {
        // TabNavigator
        Tab: { 
            screen: TabNavigator(
                // 配置路由
                {   
                    // 普通 Component
                    Home: { screen: HomePage },
                    Near: { screen: NearPage },
                    Order: { screen: OrderPage },
                    Mine: { screen: MinePage },
                },
                // 配置選項(xiàng)卡的通用樣式
                {
                    tabBarComponent: TabBarBottom,
                    tabBarPosition: 'bottom',
                    swipeEnabled: true,
                    animationEnabled: true,
                    lazy: true,
                    tabBarOptions: {
                            activeTintColor: color.theme,
                            inactiveTintColor: color.tabbartitle,
                            style: { backgroundColor: '#fff' }
                    }
                }
            )
        },
        // 普通的Component
        Detail: {
            screen: DetailPage
        },
        Web: {
            screen: WebPage
        }
    },
    // 配置導(dǎo)航欄的通用樣式
    {
        navigationOptions:{
            headerStyle:{backgroundColor: color.theme},
            headerBackTitle:null,
            headerTintColor:'#333',
            showIcon:true,
        },
    }
)

// app.js
render(){
    return(
        <MainNavigator/>
    );
}

注意生成的 MainNavigator 渲染必須作為根組件返回。

render(){
    return(
        <View>
            <MainNavigator/>
        </View>
    );
}

這樣渲染會(huì)有幺蛾子哦。

關(guān)于導(dǎo)航組件 StackNavigator TabNavigator api 的使用可以去官網(wǎng)查看。
react-navigator官網(wǎng)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容