react-native-navigation的迁移库

sideMenu-docs.mdx 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ---
  2. id: docs-sideMenu
  3. title: Side Menu
  4. sidebar_label: Side Menu
  5. ---
  6. SideMenu provides access to destinations in the app from an easy to access menu which is accessible to the user via horizontal swipe gesture or a menu button.
  7. ## Opening programmatically
  8. To open the side menu programmatically, you'll need to update the visible property of the side menu you'd like to open.
  9. The following snippet shows how to open the left side menu. The menu is opened by setting its visible option to visible.
  10. ```jsx
  11. Navigation.mergeOptions(componentId, {
  12. sideMenu: {
  13. left: {
  14. visible: true,
  15. },
  16. },
  17. });
  18. ```
  19. ## Opening via the hamburger menu
  20. The most common use case is to open the side menu by pressing the hamburger button at the TopBar. To achieve this, listen to the press event of the burger button, and open the side menu as shown above.
  21. ```jsx
  22. const React = require('react');
  23. const Navigation = require('react-native-navigation');
  24. const { View, Text } = require('react-native');
  25. class SideMenuCenterScreen extends React.Component {
  26. static options() {
  27. return {
  28. topBar: {
  29. leftButtons: {
  30. id: 'sideMenu',
  31. icon: require('./menuIcon.png')
  32. }
  33. }
  34. };
  35. }
  36. constructor(props) {
  37. super(props);
  38. Navigation.events().bindComponent(this);
  39. }
  40. render() {
  41. return (
  42. <View>
  43. <Text>Click the hamburger icon to open the side menu</Text>
  44. </View>
  45. );
  46. }
  47. navigationButtonPressed({ buttonId }) {
  48. if (buttonId === 'sideMenu') {
  49. Navigation.mergeOptions(this, {
  50. sideMenu: {
  51. left: {
  52. visible: true
  53. }
  54. }
  55. });
  56. }
  57. }
  58. }
  59. ```
  60. ## Pushing to the center layout from a menu
  61. A common use case when using SideMenus is to interact with the center layout, e.g. pushing a screen to a stack in the center layout when a user clicks on a button from one of the menus.
  62. In order to interact with the stack, we'll first assign it a predefined id. For example:
  63. ```js
  64. sideMenu: {
  65. center: {
  66. stack: {
  67. id: 'CenterStack',
  68. children: []
  69. }
  70. },
  71. left: {
  72. ...
  73. }
  74. }
  75. ```
  76. Now that we've defined an `id` for out stack, we can go ahead and push our screen to it. If we're pushing while the SideMenu is open, we'll need to make sure we close it by updating the visibility option of the relevant menu (left or right).
  77. ```js
  78. Navigation.push('CenterStack', {
  79. component: {
  80. name: Screens.Pushed,
  81. options: {
  82. sideMenu: {
  83. left: {
  84. visible: false
  85. }
  86. }
  87. }
  88. }
  89. });
  90. ```