我在react navigation5中使用类组件,我有两个类:
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>
)}}现在,如果我必须在另一个类中访问body的值,我该怎么做呢?
发布于 2021-01-08 02:06:45
在您的RecordList组件中,可以使用route访问参数
const RecordList = ({navigation, route})=>{
const {body} = route.params;
console.log(body)
}在基于类的组件中:
class RecordList extends Component{
render(){
const {body} = this.props.route.params;
console.log(body)
return <View><Text>{body}</Text></View>
}
}发布于 2021-01-18 18:36:15
我在drawerNavigator中使用stackNavigator,所以我将不得不使用嵌套导航:
this.props.navigation.navigate('RecordList', {screen:'Home',params :{ title: "Home"}})检索可以完全像上面Ketan的答案中那样完成。
https://stackoverflow.com/questions/65617657
复制相似问题