探索优雅的React Native侧滑抽屉组件:react-native-drawer-menu
import React, { PureComponent } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import Drawer from 'react-native-drawer-menu';
class DrawerMenuExample extends PureComponent {
constructor(props) {
super(props);
this.state = {
isOpen: false,
};
}
toggleDrawer = () => {
this.drawer.open();
};
renderNavigationView = () => {
return (
<View style={{flex: 1, backgroundColor: '#fff'}}>
<TouchableOpacity onPress={this.toggleDrawer}>
<Text>关闭侧滑</Text>
</TouchableOpacity>
</View>
);
};
renderCenterView = () => {
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text>主内容区</Text>
<TouchableOpacity onPress={this.toggleDrawer}>
<Text>打开侧滑</Text>
</TouchableOpacity>
</View>
);
};
render() {
return (
<Drawer
ref={(ref) => this.drawer = ref}
content={this.renderNavigationView()}
onClose={() => this.setState({ isOpen: false })}
onOpen={() => this.setState({ isOpen: true })}
type="displace"
open={this.state.isOpen}
tapToClose={true}
negotiatePan={true}
side="left"
>
{this.renderCenterView()}
</Drawer>
);
}
}
export default DrawerMenuExample;
这个代码示例展示了如何使用react-native-drawer-menu
库创建一个侧滑抽屉组件。通过点击主内容区中的按钮来打开侧滑抽屉,并在侧滑抽屉中有一个按钮可以关闭抽屉。这个例子简单易懂,并且展示了如何使用React组件的构造器、状态和生命周期函数。
评论已关闭