react-native - React Native:将参数传递给 Drawer.Navigator
问题描述
我可以传递道具,但由于道具是只读的,我如何在 App.js 和 DrawerContent.js 之间传递其他参数?请看下面的代码片段
使用 "@react-navigation/drawer": "^5.4.0",
应用程序.js:
const Drawer = createDrawerNavigator();
const DrawerRender = () => {
return (
<NavigationContainer>
<Drawer.Navigator drawerContent={props => <DrawerContent {...props} />}>
<Drawer.Screen name="Feed" component={Feed} />
</Drawer.Navigator>
</NavigationContainer>
);
}
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
fonts_loaded: false,
isLoggedin: false,
userData: null,
isImageLoading: false
}
}
...
render() {
if(this.state.fonts_loaded){
if(this.state.isLoggedin && this.state.userData !== null){
return (
<DrawerRender />
);
}
return (
<View style={styles.container}>
...
</View>
);
}
return (<View></View>)
}
}
抽屉内容.js:
export function DrawerContent(props) {
return(
<View style={{flex:1}}>
<DrawerContentScrollView {...props}>
<View style={styles.drawerContent}>
<View style={styles.userInfoSection}>
<View style={{flexDirection:'row',marginTop: 15}}>
<Avatar.Image
source={{
uri: <GET_URI_FROM_App.js>
}}
size={50}
/>
<View style={{marginLeft:15, flexDirection:'column'}}>
<Title style={styles.title}><GET_USERNAME_FROM_App.js></Title>
...
如何将用户名和 uri 从 App.js 传递到 DrawerContent.js?在上面的代码片段中被视为GET_URI_FROM_App.js和GET_USERNAME_FROM_App.js
解决方案
你可以使用这个:
请告诉我好不好用。
应用程序.js
const Drawer = createDrawerNavigator();
const DrawerRender = ({ passProps }) => {
return (
<NavigationContainer>
<Drawer.Navigator drawerContent={props => <DrawerContent {...props} {...passProps} />}>
<Drawer.Screen name="Feed" component={Feed} />
</Drawer.Navigator>
</NavigationContainer>
);
}
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
fonts_loaded: false,
isLoggedin: false,
userData: null,
isImageLoading: false
}
this.passProps = {
username: 'john',
uri: 'doe',
}
}
...
render() {
if(this.state.fonts_loaded){
if(this.state.isLoggedin && this.state.userData !== null){
return (
<DrawerRender passProps={this.passProps} />
);
}
return (
<View style={styles.container}>
...
</View>
);
}
return (<View></View>)
}
}
抽屉内容.js
export function DrawerContent(props) {
return(
<View style={{flex:1}}>
<DrawerContentScrollView {...props}>
<View style={styles.drawerContent}>
<View style={styles.userInfoSection}>
<View style={{flexDirection:'row',marginTop: 15}}>
<Avatar.Image
source={{
uri: props.uri
}}
size={50}
/>
<View style={{marginLeft:15, flexDirection:'column'}}>
<Title style={styles.title}>{ props.username }</Title>
...
推荐阅读
- reactjs - 由于 HTTPS 问题 MERN 无法登录 Facebook
- c - gcc 和 clang 给出不同的结果
- javascript - 在下一个 js 中为个人资料页面定义登录用户路由?
- javascript - 如何获得包含字符串和数字的数组中的平均数
- php - 如何一起使用 laravel 分页 & makeHidden 和收集方法
- python - Py2Exe:将python文件转换为可执行文件后相机不起作用
- flutter - 从 Flutter 中的另一个类访问更改的 Slider 值
- windows - 任何人都可以修复该命令,我尝试了该命令,但 CMD 命令出错,
- django - 在白色 ip 上运行 Django 应用程序
- mysql - SQL 查询在不同表之间不一致