123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- ---
- id: stack
- title: Stack
- sidebar_label: Stack
- ---
-
- import Tabs from '@theme/Tabs';
- import TabItem from '@theme/TabItem';
-
- 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.
-
-
- <Tabs
- defaultValue="single"
- values={[
- { label: 'Single child', value: 'single', },
- { label: 'Multiple Children', value: 'multiple', }
- ]
- }>
- <TabItem value="single">
-
- A stack declared with a single child.
-
- ```js
- const stack = {
- children: [
- {
- component: {
- name: 'MyComponent'
- }
- }
- ]
- }
- ```
-
- </TabItem>
- <TabItem value="multiple">
-
- 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.
-
- ```js
- const stack = {
- children: [
- {
- component: {
- name: 'RootComponent'
- }
- },
- {
- component: {
- name: 'SecondComponent'
- }
- }
- ]
- }
- ```
-
- </TabItem>
- </Tabs>
-
-
- Buttons can be added to the [right](
-
- :::tip 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.
- :::
-
- Left button
- Left buttons behave like right buttons with two caveats on Android:
- * Only a single left button is allowed
- * Textual left button isn't supported
-
- ### Using a react component in a button
- :::caution
- At the moment, custom buttons in `rightButtons` are supported only on iOS.
- :::
-
- Sometimes 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:
-
- ```js
- Navigation.registerComponent('ButtonComponent', () => require('./ButtonComponent'));
- ```
-
- Now you can create buttons which use the component registered with `'ButtonComponent'` as their custom view:
-
- ```js
- topBar: {
- rightButtons: [
- {
- component: 'ButtonComponent',
- passProps: {
-
- }
- }
- ]
- }
- ```
-
- Changing buttons dynamically
- As buttons are part of a screen's options, they can be modified like any other styling option using the [mergeOptions]() command.
-
- #### Setting buttons
- The following command will set the screen's right buttons. If the screen already has Right Buttons declared - they will be overridden.
-
- ```js
- Navigation.mergeOptions(this.props.componentId, {
- topBar: {
- rightButtons: [
- {
- id: 'myDynamicButton',
- text: 'My Button'
- }
- ]
- }
- });
- ```
-
- Styling the back button
- The 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.
-
- ```js
- backButton: {
- color: 'red',
- icon: require('../../img/customChevron.png')
- }
- ```
-
- Changing visibility programmatically
- Back 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.
-
- ```js
- Navigation.mergeOptions(this.props.componentId, {
- backButton: {
- visible: false
- }
- });
- ```
-
- Interact with the Stack by componentId
- Each 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.
-
- ```jsx
- const React = require('react');
- const Navigation = require('react-native-navigation');
-
- class MyComponent extends React.Component {
- onButtonClick = () => {
- Navigation.push(this.props.componentId, {
- component: {
- name: 'PUSHED_SCREEN'
- }
- });
- }
- }
- ```
-
- ### Interact with the Stack by a predefined id
- Sometimes 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.
-
- ```js
- Navigation.setRoot({
- root: {
- stack: {
- id: 'MyStack',
- children: [
- {
- component: {
- name: 'SomeComponent'
- }
- }
- ]
- }
- }
- });
-
- function push() {
- Navigation.push('MyStack', {
- component: {
- name: 'PushedScreen'
- }
- });
- }
- ```
-
-
-
|