ReactNative導(dǎo)航的右邊添加按鈕做跳轉(zhuǎn)

ReactNative 棧導(dǎo)航(StackNavigator):
第一步引入import {StackNavigator} from 'react-navigation';
第二步:創(chuàng)建組件設(shè)置navigationOptions
第三步:StackNavigator 配置屏幕
(1.需要做跳轉(zhuǎn)的其他組件時(shí)需要通過(guò)props獲取navigate ,
const {navigate}=this.props.navigation,然后在跳轉(zhuǎn)事件里面調(diào)用navigator('傳入要跳轉(zhuǎn)屏幕對(duì)應(yīng)的key')
2.如果在navigationOptions里面做跳轉(zhuǎn)則需要吧{navigation}傳進(jìn)去,然后代用navigation.navigate('傳入要跳轉(zhuǎn)屏幕對(duì)應(yīng)的key'),如下:
在導(dǎo)航的右邊添加headerRight 重點(diǎn)是把navigation傳進(jìn)去,比如

static navigationOptions=({navigation})=>({

title:'Title',

headerRight:<Button title='right' onPress={()=>navigation.navigate('這里是指向的組件key')}/>

});
3.需要傳參數(shù)的時(shí)候在navigate(參數(shù)一是路由名字,參數(shù)二dict傳值)
navigate('Chat',{user:'abc'})
取參數(shù)的時(shí)候 需要 const {params} = this.props.navigation.state.
比如:const {params} = this.props.navigation.state;
return <Text>Chat with{params.user}</Text>
如果是在navigationOptions取參數(shù)
比如 static navigationOptions =({navigation})=>({
title:${navigation.state.params.user},
}
)
//)。
···
/**

import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
Dimensions,
TextInput,
ScrollView,
FlatList,
SectionList,
Button
} from 'react-native';

import {StackNavigator} from 'react-navigation';

class HomeScreen extends Component{
static navigationOptions =({navigation})=>({
title:'Main',
headerRight:<Button title="Right" onPress={()=>navigation.navigate('Sec',{user:'Lucy'})}/>,
}) ;
render(){
const {navigate} = this.props.navigation;
return(
<View>
<Text>this is Main</Text>
<Button title="Second" onPress={()=>navigate('Sec',{user:'Lucy'})}/>
</View>
);
}

}
class Second extends Component{
static navigationOptions =({navigation})=>( {
title:Chat with ${navigation.state.params.user},
});
render(){
const {params} = this.props.navigation.state;
return(
<Text>chat with {params.user}</Text>
);
}
}
const App = StackNavigator({
Home:{screen:HomeScreen},
Sec:{screen:Second}
});

var screenWidth =Dimensions.get('window').width;
const styles = StyleSheet.create({

container:{
    flex:1,
    marginTop:20,

},
text:{
    flex:1,
    justifyContent:'center',
    alignItems:'center',
    textAlign:'center',
    backgroundColor:'red',
},

});

AppRegistry.registerComponent('AwesomeProject', () => App);
···

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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