react-native-navigation的迁移库

stack-buttons.mdx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ---
  2. id: stack-buttons
  3. title: TopBar Buttons
  4. sidebar_label: TopBar Buttons
  5. ---
  6. Buttons can be added to the [right](#rightButtons) and [left](#leftButtons) areas of the TopBar. Buttons can have either an icon or a text. They are declared in the the options object and, as with any other option, can be updated dynamically with the `Navigation.mergeOptions` command.
  7. > When using an icon button on **Android**, you should always pass a title as well. The title is used when the button is collapsed to the overflow menu and as a tooltip when the button is long pressed.
  8. # Overflow menu
  9. It's common practice to group less important actions in a menu or an action sheet.
  10. To do so on iOS, include a button with a menu icon and open an [ActionSheet](https://facebook.github.io/react-native/docs/actionsheetios) with the relevant actions when the button is clicked.
  11. On Android, use the [showAsAction](#showasaction) options to control when the button should appear in the menu.
  12. # Left button
  13. Left buttons behave like right buttons with two caveats on Android:
  14. * Only a single left button is alowed
  15. * Textual left button isn't supported
  16. # Using a react component in a button
  17. > ⚠️At the moment, Android only supports using custom buttons in `rightButtons`.
  18. Sometimes we require more from our buttons. In order to support every product need React Components can be used as custom views of buttons.
  19. To do so, you'll first need to register the view with Navigation, just like you register your components used as screens:
  20. ```js
  21. Navigation.registerComponent('ButtonComponent', () => require('./ButtonComponent'));
  22. ```
  23. Now you can create buttons which use the component registered with `'ButtonComponent'` as thier custom view:
  24. ```js
  25. topBar: {
  26. rightButtons: [
  27. {
  28. component: 'ButtonComponent',
  29. passProps: {
  30. // Pass initial props to the button here
  31. }
  32. }
  33. ]
  34. }
  35. ```
  36. ## Updating props of custom buttons
  37. To update props of a mounted compoennt used as a button, you'll first need to assign it a unique id, then call the `Navigation.updateProps()` command with the id.
  38. Calling the updateProps command will trigger React's component lifecycle methods related to [props update](https://reactjs.org/docs/react-component.html#updating)
  39. ```js
  40. // Declare the button and assign it a unique id
  41. topBar: {
  42. rightButtons: [
  43. {
  44. id: 'SomeUniqueId',
  45. component: 'ButtonComponent',
  46. passProps: {
  47. count: 0
  48. }
  49. }
  50. ]
  51. }
  52. // Update props
  53. Navigation.updateProps('SomeUniqueId', {
  54. count: 1
  55. });
  56. ```
  57. # Changing buttons dynamically
  58. As buttons are part of a screen's options, they can be modified like any other styling option using the [mergeOptions]() command.
  59. ## Setting buttons
  60. The following command will set the screen's right buttons. If the screen already has Right Buttons declared - they will be overridden.
  61. ```js
  62. Navigation.mergeOptions(this.props.componentId, {
  63. topBar: {
  64. rightButtons: [
  65. {
  66. id: 'myDynamicButton',
  67. text: 'My Button'
  68. }
  69. ]
  70. }
  71. });
  72. ```
  73. ## Removing buttons
  74. Buttons can be removed by setting zero buttons, as shown in the snippet below.
  75. ```js
  76. Navigation.mergeOptions(this.props.componentId, {
  77. topBar: {
  78. rightButtons: []
  79. }
  80. });
  81. ```