javascript - 反应导航中的 contentComponent 上未定义道具
问题描述
我正在尝试在我的抽屉上添加一个注销按钮,所以我使用 contentComponent 来单独自定义创建注销(而不是其他抽屉菜单)。所以,我创建了一个类并试图接收 DrawerItems 但它说没有定义道具。请帮忙。
我的代码自定义组件:
class DrawerComponent extends Component {
constructor(props) {
super(props)
console.log(this.props)
}
render () {
return (
<View style={{flex:1}}>
<SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
<DrawerItems {props.children} />
<TouchableOpacity onPress={() => console.log("Pressed")}>
<Text>
Logout
</Text>
</TouchableOpacity>
</SafeAreaView>
</View>
);
}
};
我的抽屉选项:
const DrawerNavigator = createDrawerNavigator({
Dashboard: {
screen: AppStackNavigator,
navigationOptions: {
drawerLabel: "Home",
}
},
Training: {
screen: Training,
navigationOptions: {
drawerLabel: "Training"
}
},
RoutePlan: {
screen: RouteCalendar,
navigationOptions: {
drawerLabel: "Route Plan"
}
},
Logout: {
screen: StoreList,
navigationOptions: {
drawerLabel: "Logout"
}
}
},
{
contentOptions: {
activeTintColor: '#127CC1',
},
contentComponent: props => <DrawerComponent {...props}/>,
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle',
navigationOptions : ({navigation}) => {
const { routeName } = navigation.state.routes[navigation.state.index];
const headerTitle = routeName;
return {
headerTitle,
headerLeft: (
<Icon name="md-menu" style={{ marginLeft: 10 }}
onPress={() => navigation.toggleDrawer()}
/>
)
}
}
}
);
在我的 customComponent 页面上出现错误。请帮助我度过难关!
解决方案
在您的render
方法中DrawerComponent
,您正在调用props.children
. 在那种情况下,props
还没有在 render 方法中定义。您可以let props = this.props
在 render 方法的开头使用一行来定义它,或者只是调用this.props
而不是裸props
.
如果您改用功能组件,则不需要从 获取props
,this
而是将其作为功能组件的参数:
function FooComponent(props){
return (<div style={{color:"#F00"}}>props.children</div>)
}
ReactDOM.render(<FooComponent><em>Hi!</em></FooComponent>, targetElement)
推荐阅读
- r - R包cem和MatchIt:不同的不平衡度量
- java - 处理(Drop and Log) Kafka producer 发布的不良数据,这样 Spark (Java) Consumer 不会将其存储在 HDFS 中
- node.js - 如何在multer中将cb错误转换为json格式
- c - 错误:构建 AV1 AOM 项目时将 R_X86_64_32 重新定位到“.rodata”
- java - 如何定位哪些库不兼容?尝试更新 Spring Boot 时出现问题
- swift - 如何快速创建一个不能为负的 int
- c - C中的递归 - 数组的最大元素
- python - ctypes._FuncPtr 接受哪个偏移量作为输入?
- python-3.x - Python设置突变:AttributeError:'set'对象没有属性'k'
- xamarin.forms - 如何在 Xamarin Forms 中保存会话存储?