React 导航 5:使用导航传递参数
React navigation 5 : Pass arguments with Navigation
我正在使用 class 组件和 react navigation5,我有两个 classes :
Class DrawerComponent.js
export default class DrawerContent extends Component{
constructor(props){
super(props);
}
render(){
return(
<View style={{flex:1}}>
<DrawerContentScrollView {...this.props}>
<Drawer.Section style={styles.drawerSection}>
{
<DrawerItem
icon={({color,size}) => (
<Icon
name=""
color={color}
size={size}
/>
)}
label={menu.localizedTitle}
onPress = {() =>**{this.props.navigation.navigate("RecordList",{body :'abc' }**)}}/>)
</Drawer.Section>
</View>
</DrawerContentScrollView>
</View>
)}}
现在如果我必须访问另一个 class 中的 body 值,我该怎么做?
在您的 RecordList
组件中,您可以使用 route
访问参数
const RecordList = ({navigation, route})=>{
const {body} = route.params;
console.log(body)
}
基于 class 的组件:
class RecordList extends Component{
render(){
const {body} = this.props.route.params;
console.log(body)
return <View><Text>{body}</Text></View>
}
}
我在 drawerNavigator 中使用 stackNavigator,因此我将不得不使用嵌套导航:
this.props.navigation.navigate('RecordList', {screen:'Home',params :{ title: "Home"}})
并且可以完全按照上面 Ketan 的回答中的方式进行检索。
我正在使用 class 组件和 react navigation5,我有两个 classes :
Class DrawerComponent.js
export default class DrawerContent extends Component{
constructor(props){
super(props);
}
render(){
return(
<View style={{flex:1}}>
<DrawerContentScrollView {...this.props}>
<Drawer.Section style={styles.drawerSection}>
{
<DrawerItem
icon={({color,size}) => (
<Icon
name=""
color={color}
size={size}
/>
)}
label={menu.localizedTitle}
onPress = {() =>**{this.props.navigation.navigate("RecordList",{body :'abc' }**)}}/>)
</Drawer.Section>
</View>
</DrawerContentScrollView>
</View>
)}}
现在如果我必须访问另一个 class 中的 body 值,我该怎么做?
在您的 RecordList
组件中,您可以使用 route
const RecordList = ({navigation, route})=>{
const {body} = route.params;
console.log(body)
}
基于 class 的组件:
class RecordList extends Component{
render(){
const {body} = this.props.route.params;
console.log(body)
return <View><Text>{body}</Text></View>
}
}
我在 drawerNavigator 中使用 stackNavigator,因此我将不得不使用嵌套导航:
this.props.navigation.navigate('RecordList', {screen:'Home',params :{ title: "Home"}})
并且可以完全按照上面 Ketan 的回答中的方式进行检索。