react-native-navigation icon indicating copy to clipboard operation
react-native-navigation copied to clipboard

[V2] need more complete example for sideMenu in docs

Open jerbombs opened this issue 8 years ago • 29 comments

Issue Description

After reading the docs and checking the code I am still stuck in understanding how to make a layout where you can navigate to a screen from side menu.

Steps to Reproduce / Code Snippets / Screenshots

I have this function I call from a button in side menu component.

goToContactUS = () => {
   console.log(this.props.componentId)
  Navigation.push(this.props.componentId, {
     component: {
         name: 'screen.ContactUS',
     }
   })
}

Currently this is my layout

Navigation.setRoot({
  root: {
    sideMenu: {
      left: {
        component: {
          name: 'screen.SideMenu',
        }
      },
      center: {
        bottomTabs: {
          id: 'tabs',
          options: {
            topbar: {
              visible: true,
            }
          },
          children: [
            {
              stack: {
                id: 'tab1',
                children: [
                  {
                    component: {
                      name: 'screen.Home',
                      options: {
                        topbar: {
                          visible: true
                        },
                        bottomTab: {
                          fontSize: 12,
                          text: 'Home',
                          icon: homeIcon,
                          textColor: theme.$tabColor,
                          selectedTextColor: '#000',
                          selectedIconColor: '#000',
                        }
                      }
                    },
                  },
                ]
              }
            },
            {
              stack: {
                id: 'tab2',
                children: [
                  {
                    component: {
                      name: 'screen.Events',
                      options: {
                        bottomTab: {
                          text: 'Events',
                          fontSize: 12,
                          icon: eventsIcon,
                          textColor: theme.$tabColor,
                          selectedTextColor: '#000',
                          selectedIconColor: '#000',
                        }
                      }
                    },
                  },
                ]
              }
            },
          ],
        },
      }
    }
  }
});

How do I make a layout where in I can navigate to the ContactUs screen from Side menu component?


Environment

  • React Native Navigation version: 2.0.2474
  • React Native version: 0.56
  • Platform(s) (iOS, Android, or both?): both
  • Device info (Simulator/Device? OS version? Debug/Release?): dev

jerbombs avatar Aug 23 '18 14:08 jerbombs

@jerbombs - You need to pass the componentId of the screen on top of which you want to push the new screen. Say, for example, you want to push on top of the tab1, try putting tab1 as your componentId in the push.

rgoldiez avatar Aug 23 '18 16:08 rgoldiez

@rgoldiez thanks for reply, but what if i want to add a screen outside of the tabs. for example I have 3 buttons in side menu. logout, contactus, and profile. and I want to navigate to contactus? Where should I put the ContactUS component here? inside the center?

jerbombs avatar Aug 24 '18 00:08 jerbombs

@jerbombs - Try adding a stack under center and add id to the stack object. You should be able to setStackRoot() using the newly added stack id or you could push a screen onto the stack (over the bottomTabs.

rgoldiez avatar Aug 24 '18 01:08 rgoldiez

i tried this

center: {
        stack: {
           id: 'dummyStack',
          children: [
             {
                component: {
                   name: 'screen.ContactUs'
                }
             },
             {
                
        bottomTabs: {
          id: 'tabs',
          options: {
            topbar: {
              visible: true,
            }
          },
          children: [
            {
              stack: {
                id: 'tab1',
                children: [
                  {
                    component: {
                      name: 'screen.Home',
                      options: {
                        topbar: {
                          visible: true
                        },
                        bottomTab: {
                          fontSize: 12,
                          text: 'Home',
                          icon: homeIcon,
                          textColor: theme.$tabColor,
                          selectedTextColor: '#000',
                          selectedIconColor: '#000',
                        }
                      }
                    },
                  },
                ]
              }
            },
            {
              stack: {
                id: 'tab2',
                children: [
                  {
                    component: {
                      name: 'screen.Events',
                      options: {
                        bottomTab: {
                          text: 'Events',
                          fontSize: 12,
                          icon: eventsIcon,
                          textColor: theme.$tabColor,
                          selectedTextColor: '#000',
                          selectedIconColor: '#000',
                        }
                      }
                    },
                  },
                ]
              }
            },
          ],
        },
             }
          ]
        },

but I still couldn't Navigate.push to screen.ContactUS and the bottomtab is a mess.

@rgoldiez can you give me an example of sideMenu layout ? the example for the sideMenu in the docs doesn't feel complete

jerbombs avatar Aug 24 '18 03:08 jerbombs

still no progress :(

jerbombs avatar Aug 25 '18 16:08 jerbombs

Hi, I have the same layout as yours, its not a question about "placing" the Contact Us component somewhere in the stack, its about in which stack and from which component are you pushing from?

pushAndCloseSideMenu() {
	this.hideSideMenu();
	Navigation.push('tab1', {
		component: {
			name: screen.ContactUS,
		},
	});
}

The above will push ContactUs from tab1. So even if you are in tab2, and you use the SideMenu to navigate, Contact US will be shown, but in tab1. [You would have to switch to tab1 to see ContactUS]

The only problem I am having with this combination of bottomTabs and SideMenu is passing the tab component Id in SideMenu component. I would have to use some kind of state management (redux) to track which tab I am currently in now, so that when I show SideMenu and push to ContactUS, I would push from the active tab I am in now.

Anarcho-X avatar Aug 28 '18 08:08 Anarcho-X

Following up on my recent post, I found a simple solution to handle active SideMenu navigation with bottomTabs.

I use a global variable that I define here:

registerScreens(); global.currentSelectedTabIndex = 0; // set default as no tab is clicked on app load Navigation.events().registerBottomTabSelectedListener( ({ selectedTabIndex, unselectedTabIndex }) => (global.currentSelectedTabIndex = selectedTabIndex) );

This would require all my tabs to have IDs in this format ('tab0', 'tab1', 'tab2', ...)

So, in my SideMenu component, to navigate to any other component, I can do this:

pushAndCloseSideMenu() { this.hideSideMenu(); Navigation.push('tab'.concat(global.currentSelectedTabIndex.toString()), { component: { name: screen.ContactUS }, }); }

Anarcho-X avatar Aug 28 '18 08:08 Anarcho-X

Have you checked here? Then just

Navigation.push(center_stack_id,{
component{
    name: registeredComponentName
})

angelos3lex avatar Aug 31 '18 06:08 angelos3lex

@spiraltrip @angelos3lex thanks for you reply. @spiraltrip I kind of understand it now. But what If I want contact page to be on top the bottom tabs? How do I design the layout? I tried putting it inside the stack in the center

like this

center: {
        stack: {
           id: 'dummyStack',
          children: [
             {
                component: {
                   name: 'screen.ContactUs'
                }
             },
             {
                
        bottomTabs: {
          id: 'tabs',
          options: {
            topbar: {
              visible: true,
            }
          },
          children: [
            {
              stack: {
                id: 'tab1',
                children: [
                  {
                    component: {
                      name: 'screen.Home',
                      options: {
                        topbar: {
                          visible: true
                        },
                        bottomTab: {
                          fontSize: 12,
                          text: 'Home',
                          icon: homeIcon,
                          textColor: theme.$tabColor,
                          selectedTextColor: '#000',
                          selectedIconColor: '#000',
                        }
                      }
                    },
                  },
                ]
              }
            },
            {
              stack: {
                id: 'tab2',
                children: [
                  {
                    component: {
                      name: 'screen.Events',
                      options: {
                        bottomTab: {
                          text: 'Events',
                          fontSize: 12,
                          icon: eventsIcon,
                          textColor: theme.$tabColor,
                          selectedTextColor: '#000',
                          selectedIconColor: '#000',
                        }
                      }
                    },
                  },
                ]
              }
            },
          ],
        },
             }
          ]
        },

but the topbar is not looking good. there is no title anymore just this. My topbar should be yellow. Seem like it is on top of another topbar

screen shot 2018-09-02 at 12 24 50 pm

jerbombs avatar Sep 02 '18 04:09 jerbombs

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest version and report back. Thank you for your contributions.

stale[bot] avatar Oct 17 '18 04:10 stale[bot]

The issue has been closed for inactivity.

stale[bot] avatar Oct 24 '18 05:10 stale[bot]

Hi all, any luck with solving this puzzle? I have a sideMenu and cannot find any documentation/example on how to switch between the screens properly

alexsmartens avatar Nov 04 '18 23:11 alexsmartens

@alexsmartens - What screens are you trying to switch between? For me, I have a stack in my center view and issue a Navigation.setStackRoot(CENTER_STACK_ID, {options}) to switch the stack in the center view.

rgoldiez avatar Nov 04 '18 23:11 rgoldiez

@rgoldiez, I have a side menu with some number of screens. I want to navigate between these screens. I am trying to do exactly what you described, but cannot make it work. Don't you mind posting a snippet of your code showing how you construct your stack and navigate between the screens?

alexsmartens avatar Nov 05 '18 00:11 alexsmartens

See my initial layout below. One key point (not well documentation) is to assign an id to the center stack if you want to perform setStackRoot. Then, when your user interacts with the sideMenu items, you can issue a Navigation.setStackRoot('stackRoot', {options}) (in my case) to change the center screen.

Navigation.setRoot({
  root: {
    sideMenu: {
      left: {
        component: {
          name: 'leftDrawer',
          id: 'leftDrawer',
        },
      },
      center: {
        stack: {
          id: 'stackRoot',
          children: [
            {
              component: {
                name: 'entry',
              },
            },
          ],
        },
      },
    },
  },
});

rgoldiez avatar Nov 05 '18 00:11 rgoldiez

@rgoldiez, Thank you for your explanation! Now screen switching works in my implementation.

So, in this case I don't need to list all my components (screens) in center stack. I do add only my initial component (screen) to the center stack and do Navigation.setStackRoot('stackRoot', {options}) when I need to switch to a different screen. Does it sound reasonable?

alexsmartens avatar Nov 05 '18 00:11 alexsmartens

@alexsmartens - You've got it exactly right. The center stack just has in the stack what's needed right now (possibly just one screen). Your center screen could push more onto that stack. Then a subsequent sideMenu "navigation" would set a new stack (again, possibly just a single screen) to replace the center view.

rgoldiez avatar Nov 05 '18 01:11 rgoldiez

Please help me to switch two screens using setStackRoot

The given below structure is my project navigation. Then I want to switch from tab page to a profile page. how can I set setStackRoot

Navigation.setRoot({
      root: {
        sideMenu: {
          left: {
            component: {
              name: 'DrawerScreen',
            },
          },
          center: {
            id: 'MY_STACK',
            bottomTabs: {
              children: [{
                stack: {
                  children: [{
                    component: {
                      name: 'HomeScreen',
                    }
                  }],
                  options: {
                    bottomTab: {
                      text: 'Home'
                    }
                  }
                }
              },{
                stack: {
                  children: [{
                    component: {
                      name: 'TabSettingsScreen',
                      passProps: {
                        text: 'This is tab 1'
                      }
                    }
                  }],
                  options: {
                    bottomTab: {
                      text: 'Setings'
                    }
                  }
                }
              }]
            }
          },
        },
      },
    })

Your center screen MY_STACK is bottomTabs not a stack. You can only use setStackRoot on stacks, not tabs. You’ll need to wrap your bottomTabs in a Stack.

rgoldiez avatar Nov 05 '18 09:11 rgoldiez

@rgoldiez Thanks, can you give an example for this.