react-navigation 4.x的使用

1. 安裝

先根據文檔進行添加依賴庫

yarn add react-navigation
yarn add react-native-gesture-handler
yarn add react-native-reanimated
yarn add react-native-screens
yarn add react-navigation-stack

然后iOS 版本

cd iOS
pod install

參考官網 : https://reactnavigation.org/docs/en/getting-started.html

  1. 使用
class HomeScreen extends React.Component{
    componentDidMount() {
        console.log("HomeScreen_componentDidMount");
    }

    componentWillUnmount() {
        console.log("HomeScreen_componentWillUnmount");
    }
    

  render(){
    return <View>
        <Text>首頁</Text>
        <Button title="跳轉" onPress={()=>this.props.navigation.navigate("detail")}>

        </Button>
    </View>
  }
}

導航生命周期

componentDidMount 頁面被加載的時候調用,類似于iOS里面的initialize方法

componentWillUnmount當頁面出棧的時候被調用, 類似iOS里面的 dealloc方法

屏幕切換

this.props.navigation.navigate("組件路由名字")
this.props.navigation.push("組件路由名字")
this.props.navigation.goBack("組件路由名字")
this.props.navigation.popToTop("組件路由名字")

push : 創(chuàng)建一個新的組件,進行壓棧展示
navigate: 會判斷棧中有沒有這個組件, 如果有則回到那個頁面,如果沒有則創(chuàng)建一個新的組件進行壓棧展示
popToTop : 回到首頁組件
goBack : 返回上一個頁面

頁面之間傳遞參數(shù)
navigationOptions 里面可以設置導航標題 ,也可以使用函數(shù)的方式后去

//方式1:
  static navigationOptions = {
        title : "首頁",
   }

//方式2
static  navigationOptions = ({navigation}) => {
        return {title : navigation.getParam("otherParamsrrr","默認列表")}
    }

this.props.navigation.navigate 方法可以傳遞參數(shù)到下一個頁面

class  HomeScreen extends React.Component{

    static navigationOptions = {
        title : "首頁",
   }

    render(){
        return <View>
            <Text>首頁</Text>
            <Button title="進入列表" onPress={()=>this.props.navigation.navigate("List",{
                "itemId" : 98,"otherParamsrrr" : "列表"
            })}></Button>
        </View>
    }
}

獲取標題 navigation.getParam
render函數(shù)里面定義 const {navigation} = this.props; 通過navigation進行獲取
也可以通過this.props.navigation.setParams進行動態(tài)修改

class ListScreen extends React.Component{

    static  navigationOptions = ({navigation}) => {
        return {title : navigation.getParam("otherParamsrrr","默認列表")}
    }

    render(){

        const {navigation} = this.props;

        return <View>
            <Text>列表</Text>
            <Button
                title="返回首頁"
                onPress={()=>this.props.navigation.navigate("Home")}></Button>
            <Button title="進入詳情" onPress={()=>this.props.navigation.navigate("Detail")}></Button>
            <Button
                title="更新標題"
                onPress={() => this.props.navigation.setParams({ otherParamsrrr: 'Updated!' })}
            />

            <Text>
                itemId: {JSON.stringify(navigation.getParam('itemId', 'NO-ID'))}
            </Text>
            <Text>
                otherParam:
                {JSON.stringify(navigation.getParam('otherParamsrrr', 'default value'))}
            </Text>
        </View>
    }
}

配置導航欄顏色,標題樣式
對于單個導航可以在當前的組件里面通過navigationOptions 配置headerStyle,headerTintColor,headerTitleStyle

class  HomeScreen extends React.Component{

    static navigationOptions = {
        title : "首頁",
        headerStyle: {
            backgroundColor : "red"
        },
        headerTintColor : "blue",
        headerTitleStyle : {
            fontSize : 30
        }

    }

    render(){
        return <View>
            <Text>首頁</Text>
            <Button title="進入列表" onPress={()=>this.props.navigation.navigate("List",{
                "itemId" : 98,"otherParamsrrr" : "列表"
            })}></Button>
        </View>
    }
}

也可以在createStackNavigator里面進行全局配置

const APPNavigator = createStackNavigator({
    Home : {
        screen : HomeScreen,
    } ,

    List : {
        screen : ListScreen,
    },

    Detail : {
        screen: DetailScreen,
    },

},{

    initialRouteName: 'Home',
    defaultNavigationOptions : {
        headerStyle: {
            backgroundColor: "red"
        },
        headerTintColor: "blue",
        headerTitleStyle: {
            fontSize: 30
        },
    }
});
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市帜慢,隨后出現(xiàn)的幾起案子僵井,更是在濱河造成了極大的恐慌擦盾,老刑警劉巖尊搬,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掂碱,死亡現(xiàn)場離奇詭異敲街,居然都是意外死亡扫腺,警方通過查閱死者的電腦和手機愁茁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門蚕钦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人埋市,你說我怎么就攤上這事冠桃。” “怎么了道宅?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵食听,是天一觀的道長。 經常有香客問我污茵,道長樱报,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任泞当,我火速辦了婚禮迹蛤,結果婚禮上,老公的妹妹穿的比我還像新娘襟士。我一直安慰自己盗飒,他們只是感情好,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布陋桂。 她就那樣靜靜地躺著逆趣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪嗜历。 梳的紋絲不亂的頭發(fā)上宣渗,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天抖所,我揣著相機與錄音,去河邊找鬼痕囱。 笑死田轧,一個胖子當著我的面吹牛,可吹牛的內容都是我干的鞍恢。 我是一名探鬼主播傻粘,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼有序!你這毒婦竟也來了抹腿?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤旭寿,失蹤者是張志新(化名)和其女友劉穎警绩,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盅称,經...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡肩祥,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了缩膝。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片混狠。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖疾层,靈堂內的尸體忽然破棺而出将饺,到底是詐尸還是另有隱情,我是刑警寧澤痛黎,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布予弧,位于F島的核電站,受9級特大地震影響湖饱,放射性物質發(fā)生泄漏掖蛤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一井厌、第九天 我趴在偏房一處隱蔽的房頂上張望蚓庭。 院中可真熱鬧,春花似錦仅仆、人聲如沸器赞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拳魁。三九已至,卻和暖如春撮弧,著一層夾襖步出監(jiān)牢的瞬間潘懊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工贿衍, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留授舟,地道東北人。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓贸辈,卻偏偏與公主長得像释树,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子擎淤,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354