Having two menus displayed simultaneously on the screen is not feasible

Achieving the Desired Screen Layout

To create a screen layout with two menus, one being the burger menu generated using createDrawerNavigator from react-navigation (located in the top left corner) and the other menu as a bottom button created by createBottomTabNavigator, I have encountered some challenges.

Although having two menus might not be the ideal UI choice, it is a requirement set by my company. Additionally, both menus share at least one common entry point which is the homepage.

https://i.sstatic.net/L85Yu.png https://i.sstatic.net/iGzD0.png

Attempts Made So Far

In my quest to achieve this layout, I came across a related question on Stack Overflow that provided some insight but did not fully address my specific requirements.

I experimented with various hierarchical structures without success in meeting the desired goal.

Subsequently, unable to navigate to certain routes like 'cart' and 'search' on my screen due to undefined routes within the navigation system provided by react-navigation.

This led me to customize the bottom menu myself, which seems to be working well, albeit missing key navigable routes.

It appears that react-navigation utilizes its components to define routes.

The Question at Hand

How can I incorporate additional routes into the navigation system for seamless navigation?

I've tried:

  • Specifying an initialState for the <NavigationContainer> but to no avail.

  • Creating a custom router to override the getInitialState method, yet struggling to integrate my custom router thereafter!

Any assistance or guidance on this matter would be greatly appreciated.

Edit:

The navigation code

// src/menu/burger/drawer.js
import { createDrawerNavigator } from '@react-navigation/drawer';
import { useTranslation } from 'react-i18next';
import Ionicons from '@expo/vector-icons/Ionicons';
import React from 'react';

import Account from '../../pages/account';
import Brands from '../../pages/brands';
import Cart from '../../pages/cart';
import Homepage from '../../pages/homepage';
import {
  ACCOUNT,
  BRANDS,
  CART,
  HOMEPAGE,
} from '../../pages/pageIdentifiers';

const Drawer = createDrawerNavigator();
const renderMenuIcon = (iconName, size, focused) => (
  <Ionicons
    name={iconName}
    size={size}
    color={focused ? '#7cc' : '#ccc'}
  />
);

// This component render the items of the burger menu displayed in the top left corner
export default function MenuDrawer() {
  const { t } = useTranslation();

  return (
    <Drawer.Navigator
      initialRouteName={HOMEPAGE}
    >
      <Drawer.Screen
        name={HOMEPAGE}
        component={Homepage}
        options={{
          title: t('menu.item.homepage'),
          drawerIcon: ({ focused, size }) => renderMenuIcon('md-home', size, focused),
        }}
      />
      <Drawer.Screen
        name={CART}
        component={Cart}
        options={{
          title: t('menu.item.cart'),
          drawerIcon: ({ focused, size }) => renderMenuIcon('cart-outline', size, focused),
        }}
      />
      <Drawer.Screen
        name={ACCOUNT}
        component={Account}
        options={{
          title: t('menu.item.account'),
          drawerIcon: ({ focused, size }) => renderMenuIcon('person-outline', size, focused),
        }}
      />
      <Drawer.Screen
        name={BRANDS}
        component={Brands}
        options={{
          title: t('menu.item.brands'),
          drawerIcon: ({ focused, size }) => renderMenuIcon('basket-outline', size, focused),
        }}
      />
    </Drawer.Navigator>
  );
}


// App.js
export default function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <MenuDrawer />
        <BottomTabsMenu />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

// src/menu/bottomTabs/drawer.js

export default function BottomTabsMenu() {
  const [activeTab, setActiveTab] = useState(HOMEPAGE);
  const navigation = useNavigation();
  
  const onMenuPress = (id) => {
    navigation.navigate(id);
    setActiveTab(id);
  };

  return (
    <View style={styles.container}>
      {bottomMenuItems.map(
        (menuItem) => (
          <View
            key={`view-${menuItem.id}`}
            style={styles.item}
          >
            <MenuItem
              key={`menu-${menuItem.id}`}
              isFocus={menuItem.id === activeTab}
              onPress={(id) => onMenuPress(id)}
              configuration={menuItem}
            />
          </View>
        )
      )}
    </View>
  );
}

Answer №1

Check out the minimized version here: https://snack.expo.dev/BsCtDUFoM
For the full/original version, click here: https://snack.expo.dev/Zgn9M59H7

On the web, both navigators and menus are in perfect sync as you navigate.

(Update: Successfully synced for both android and ios)

The code may be messy, but I'm not sure how to clean it up at this point.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to have both Node.js and browser code in the same file using webpack, while ensuring that only the browser code is accessible and the Node.js code remains hidden?

I need to work with a file that contains both Node.js and browser code. It's crucial that the Node.js code remains hidden when running in the browser environment. Is it possible for Webpack to exclude specific sections of the code based on the enviro ...

Generating option list from API JSON responses

I am currently developing a news app using React. I have set up my API to fetch data from newsapi.org and my goal is to display the available news source options in a dropdown list within my select component. However, instead of listing all news sources w ...

Exploring the seamless integration of the Material UI Link component alongside the Next.JS Link Component

Currently, I am integrating Material-UI with Next.js and would like to leverage the Material-UI Link component for its variant and other Material UI related API props. However, I also require the functionality of the Next.js Link component for navigating b ...

Error message displayed when pressing a button on React Native: "Uncaught Error in Redux - Actions must be plain objects. To handle async actions, use custom middleware."

Recently, I delved into the world of React-Native and Redux. While most of the integration went smoothly, I encountered a roadblock when trying to dispatch actions. Every attempt resulted in the error 'Redux Uncaught Error: Actions must be plain objec ...

Making an "associated" route the active route within Aurelia

In my Aurelia application, I have implemented two routes: a list route called Work and a detail route called WorkDetail. Currently, only the list route is visible in the navigation menu: Home | *Work* | Contact | . . . When users navigate to the W ...

How to Spin an Image in the Canvas Using HTML5

I am having trouble rotating an image inside a canvas after researching similar questions on Stack Overflow. Here's what I've learned: It seems I cannot rotate a single object inside the canvas. I can only rotate the entire canvas itself. ...

One or multiple web browsers set in the Browserslist of the project

I have recently started working with the Ionic framework and encountered some warnings while setting up my application. Can anyone help me with a solution? [ng] When starting the Ionic application, I received the following warnings: [ng] One or more browse ...

Discovering details regarding cookies established by an external domain

Is it possible to retrieve the host address of the domain that created a cookie on my webpage? Here is the scenario I am facing: I am on "domain A" and have a script linked from "domain B". A method on "domain B" sets a cookie on my "domain A". How can ...

Create new <div> elements dynamically within a loop using the value of a variable as a condition

I am in need of assistance with a function that generates news tiles ("cards") as displayed below: renderNews = <div className={styles["my-Grid-col"] + " " + styles["col-sm12"]+ " " + styles["col-md12"] + " " + styles["col-lg12"] + " " + styles["col-xl ...

What is causing only one route to be functional with the Backbone Router?

In the segment below, I have incorporated a variety of routes and view events created using Backbone.js. All the routes seem to be working as expected except for the last one 'products'. Initially, I had it set up differently but noticed that it ...

"Utilizing Trackball controls, camera, and directional Light features in ThreeJS version r69

I am struggling to synchronize trackball controls and camera with the directional light. Here is my situation: I start by initializing an empty scene with a camera, lights, and controls. Then, I load a bufferGeometry obj, calculate its centroid, and adjus ...

Switching from PHP to JavaScript before returning to PHP to establish and manage sessions

Currently, I am in the process of resolving an issue I am facing. The problem arises when utilizing the following code for someone trying to sign into the admin panel: <script> function myFunction() { //alert('you can type now, end ...

Scrolling vertically without the appearance of a scrollbar

I've searched all over the internet for a solution to my problem, but nothing seems to work for me. I want to keep vertical scrolling ability while hiding the scrollbar from view in the y direction all the time. The challenge is to make my #content-m ...

Sending a list of data using ajax to an external PHP script for deferred rendering in datatables

I've set up a form on one of my pages that features check boxes linked to the primary id of certain information that is retrieved on another page based on whether the check box was checked or not. After switching to jquery-datatables with deferred ren ...

Check out the HTML display instead of relying on console.log

Need help displaying the result of a JavaScript statement in an HTML page instead of console.log output. I am new to coding! Here is the JS code snippet: $.ajax({ url: 'xml/articles.json', dataType: 'json', type: ...

Pass PHP date to JavaScript and increase its value

I'm looking to retrieve the server time with PHP and store it in a JavaScript variable. Once stored, I'd like it to continuously increment. Below is the code snippet: function initTime(date){ var today=new Date(date); var h=today.getHours(); v ...

Discovering the key to selecting a row by double-clicking in Vuetify's v-data-table

I'm having trouble retrieving the row by event in my v-data-table. It only gives me the event and remains undefeated. How can I catch items in the v-data-table? <v-data-table :headers="showHeaders" :page="page&quo ...

Choose all the alternative A radio buttons utilizing JavaScript

If the "4 stars" option is selected at the top, I want all corresponding "4 stars" options in the form to be automatically selected. Similarly, if "3 stars" is chosen, I need all the "3 stars" options to be selected. I attempted to achieve this functionali ...

Angular JS: Automatically toggle the visibility of a div on a panel click event

There is a row, panel, and a closed div on load. Clicking on the panel should make the div appear while making the row and panel disappear. Clicking on X should make the panel and row appear again. <script src="https://ajax.googleapis.com/ajax/libs/ ...

Troubleshooting Database Saving Problem with Vue.js and Ruby on Rails

Trying to configure a nested form with Vue.js in a Rails application to save ingredients for a recipe. While all other details of the ingredients are saving correctly, the ingredient name (from an input field) is not getting saved in the database. How can ...