Stack
A stack is a container layout promoting a hierarchical navigation. It is used for navigating between screens at consecutive levels of hierarchy, steps in a flow or across an app.
The first child in the stack (represented by the children
array) is the root and is displayed at the bottom of the stack. The last child in the children array is the child currently being displayed.
In this layout, only a single child screen is visible at any given time and consecutive screen can be added to the top of the stack using the Navigation.push
command. Tapping the back button will pop the stack and remove the top most screen.
The stack manages the TopBar at the top of the stack. The TopBar displays the current screens' title and buttons. It can be hidden with the topBar: { visible: false }
option. By default, screens are rendered below the TopBar. This behavior can be changed by setting topBar: { drawBehind: true }
in the current screens' options.
#
Layout Examples- Single child
- Multiple Children
A stack declared with a single child.
A stack can be initialized with more than one child, in which case the last child will be the currently displayed child and the first child will be hidden. In this case the back button will be visible automatically, clicking it will go back in the stack revealing the first (previous) child. Once the root child becomes visible, the back button is hidden.
#
TopBar ButtonsButtons can be added to the right and left 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.
Always assign titles to buttons!
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.
#
Overflow menuIt's common practice to group less important actions in a menu or an action sheet.
To do so on iOS, include a button with a menu icon and open an ActionSheet with the relevant actions when the button is clicked.
On Android, use the showAsAction options to control when the button should appear in the menu.
#
Using a react component in a buttonSometimes we require more from our buttons. In order to support every product need, React Components can be used as custom views of buttons. To do so, you'll first need to register the view with Navigation, just like you register your components used as screens:
Now you can create buttons which use the component registered with 'ButtonComponent'
as their custom view:
#
Updating props of custom buttonsTo update props of a mounted component used as a button, you'll first need to assign it a unique id, then call the Navigation.updateProps()
command with the id.
Calling the updateProps command will trigger Reacts component lifecycle methods related to props update
#
Changing buttons dynamicallyAs buttons are part of a screen's options, they can be modified like any other styling option using the mergeOptions command.
#
Setting buttonsThe following command will set the screen's right buttons. If the screen already has Right Buttons declared - they will be overridden.
#
Removing buttonsButtons can be removed by setting zero buttons, as shown in the snippet below.
#
Back ButtonThe back button is added automatically when two or more screens are pushed into the stack.
#
Styling the back buttonThe back button's style can be customized by declaring a backButton options object. This configuration can be part of a screen's static options, or default options.
#
Controlling visibilityThe back buttons visibility can be controlled with the visible property.
#
Changing visibility programmaticallyBack button visibility can be changed dynamically using the mergeOptions command. When using a screen's componentId, the change will affect only that specific screen. But when using the stack's id, the change will affect all screens pushed into the stack.
#
Handling the back buttonHandling the back button press event is done by first disabling the default back button behaviour by adding:
As a result, pressing the back button will not pop the stack and will dispatch navigationButtonPress
event with the default back button id RNN.back
.
#
Handling the hardware back button on AndroidSame as handling the default back button, handling the hardware back button is done by disabling popStackOnPress
for the hardware button:
As a result, pressing the hardware back button will not pop the stack and will dispatch navigationButtonPress
event with the default hardware back button id RNN.hardwareBackButton
.
#
Interacting programmaticallyThe Navigation object provides ways to programmatically manipulate the stack.
#
Interact with the Stack by componentIdEach layout pushed into the stack has an id. When in the context of a component, The component's componentId
can be used to interact with a parent stack.
When using a component's componentId, the native implementation knows to perform the command on the parent Stack of this component.
In this example, we push a screen onto the component's parent stack.
#
Interact with the Stack by a predefined idSometimes we're required to interact with a specific stack not from the context of a component pushed into it. To do so, assign the stack a predefined id
and use it when invoking any stack command.