Tips for maintaining the menu state following a refresh

Is there a way to save the menu state when pressing F5? I'm looking for a similar functionality as seen on the Binance website. For example, clicking on the Sell NFT's submenu and then refreshing the page with F5 should maintain the menu state on Sell NFT's.

You can find an example at this URL: Binance

https://i.stack.imgur.com/TGMMF.png

I want to replicate this behavior in my web application.

If I click on 'Dealing'

I have two subcategories

(Dealing Overview & Order Authorization)

If I click on 'Dealing Overview' and refresh the page with F5, it resets to the initial state.

I would like to remain on

Dealing Overview & Order Authorization
.

I'm unsure how to achieve this.

Here are 2 console logs:

1/ Here, I correctly retrieve the 13 categories.

https://i.stack.imgur.com/B7aX3.png

2/ When I click on a subcategory, it displays correctly.

https://i.stack.imgur.com/q0Zgy.png

The issue is that pressing F5 resets the menu state to 0. Any ideas on how to fix this?

Here is my code

export class OnlineComponent implements OnInit {
  // Code for Online Component...
}

template

<div class="menu-content" [ngClass]="{ 'show-elem': !showElement, 'noshow-elem': showElement }">
   <ul class="menu-items">
      <li class="item" [ngClass]="{ active: item.active }" *ngFor="let item of nav">
      <div class="menu-item" [ngClass]="{ 'show-menu': showMenu, 'noshow-menu': !showMenu }">
         <span class="title" (click)="onClickChangeActiveFlag(item)">
            <span>{{ item.name }}</span>
            <fa-icon [icon]="faChevronRight"></fa-icon>
         </span>
      </div>
      </li>
   </ul>
</div>
<div [ngClass]="{ 'show-elem': showElement, 'noshow-elem': !showElement }">
   <ul class="submenu-title">
      <span class="title-sub" (click)="closeChildNav()">
         <fa-icon [icon]="faChevronLeft"></fa-icon>
         <span class="ml-2 btnBack">Retour</span>
      </span>
      <li class="items" routerLink="{{ subItem.state }}" *ngFor="let subItem of subMenu">
         <span>{{subItem.name}}</span>
      </li>
   </ul>
</div>

Answer №1

In accordance with @Mentlegen's recommendation, utilizing URL parameters could prove to be the most effective solution, provided you are comfortable with the exposure of these parameters in the URL. However, if there is a preference to not expose them for any reason, an alternative approach would involve creating a service that utilizes observables to maintain the latest menu state. This service can then be subscribed to within your component's onInit method, ensuring that the menu state is updated each time the user interacts with it.

Answer №2

When faced with this scenario, many websites typically use either the slug or the query currently present in the URL. In the instance of Binance as you mentioned, you can observe the #199 in the query that is utilized to trigger the selection effect in the side menu (try changing it to 198 and see another item being highlighted).

Therefore, in your specific situation, you should rely on your route (such as verifying if the value is demandeOrdres and highlighting the corresponding item) or include a parameter in your URL query that stores the value of the selected item.

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

Tips for transferring a file to PocketBase using NodeJs

Currently, I am in the midst of a project that necessitates uploading numerous PDF files to a PocketBase collection. All the necessary files are saved on my computer and my goal is to upload them using nodejs along with the PocketBase JavaScript SDK. Howe ...

The type '{}' cannot be assigned to the type '{ title: string; text: string; }'

Upon executing the TypeScript code below, I encountered the following error: Type '{}' is not assignable to type '{ title: string; text: string; }'. Property 'title' is missing in type '{}'. The "article" declara ...

"Upon calling an asynchronous method within another method, it appears that no progress is being displayed

I've created a `node-js` `db.js` class that retrieves an array of data for me. //db.js const mysql = require('mysql'); var subscribed = []; const connection = mysql.createConnection({ host: 'localhost', user: 'root' ...

serving files using express.static

I have set up express.static to serve multiple static files: app.use("/assets", express.static(process.cwd() + "/build/assets")); Most of the time, it works as expected. However, in certain cases (especially when downloading many files at once), some fil ...

What steps should be taken to prepare files (such as editing images) before they are uploaded to Google Drive using the `rpldy/drive-uploady` tool?

I have successfully implemented rpldy/drive-uploady to upload files to Google Drive. However, I would like to allow users to edit images before uploading them. How can I achieve this? Below is the code snippet: import DriveUploady from "drive-uploady ...

Building an array of ids that contain the checkbox type using jQuery: What's the best way?

Below is the HTML snippet I am working with: <!-- Parent checkbox code goes here --> <input id="ckbCheckAll" class="custom-check ez-hide" type="checkbox" name=""></input> <!-- Child checkbox codes follow --> <input id="practice_ ...

Encountering an excessive number of re-renders due to attempting to display a FlatList component

I am attempting to showcase a flatList of numbers similar to this: (view image example) In order to achieve this, I created an array of objects with a numberName and a key using a loop: const number = 5; let [numbers, setNumbers] = useState([]); let nums ...

What is the best way to manually decrease the speed of an object's rotation using javascript?

Can anyone assist me with slowing down the mouse-controlled rotation of a 3D object in javascript? The current rotation is too sensitive and difficult to control manually. Below is the code I am using: <html> <head> <script src="js/thr ...

How can we ensure that React state remains unaltered when modifying an array set to state?

Hope you're having a wonderful day! I'm encountering a significant problem with React. I have a state that contains an array. Within a function, I create a copy of the state in a new variable. However, any modifications made to this new variable ...

Implementing service injection within filters in NestJS

Looking to integrate nestjs-config into the custom exception handler below: import { ExceptionFilter, Catch, ArgumentsHost, Injectable } from '@nestjs/common'; import { HttpException } from '@nestjs/common'; import { InjectConfig } fro ...

Validation of dynamic fields in Angular 2 forms

I am currently working on a project using Angular 4.X where I need to dynamically create HTML input fields, mostly of type text, upon button click. Although the creation of these input boxes is functioning properly, I am facing challenges when it comes to ...

What steps are needed to build a Nested Default Dictionary in JavaScript?

I've been working on creating an Apps Script and have run into a challenge with implementing a Default Dictionary. Initially, I tried using the following code snippet: class DefaultDict { constructor(defaultInit) { return new Proxy({}, { g ...

Guide on utilizing external namespaces to define types in TypeScript and TSX

In my current project, I am working with scripts from Google and Facebook (as well as other external scripts like Intercom) in TypeScript by loading them through a script tag. However, I have encountered issues with most of them because I do not have acces ...

Error occurs in console when using .getJSON with undefined JSON, but does not happen when using embedded data

Can someone help me understand why I'm getting an 'undefined' response when I use console.log(tooltipValues), but there's no issue with console.log(tooltipJSON). I've noticed that when I embed the data directly in my JS code, ever ...

Tips for maintaining a healthy balance of tasks in libuv during IO operations

Utilizing Typescript and libuv for IO operations is crucial. In my current situation, I am generating a fingerprint hash of a particular file. Let's say the input file size is approximately 1TB. To obtain the file's fingerprint, one method involv ...

Can you verify my comprehension of the process for iteratively displaying numerous custom Tree components in Vue.js 3?

While exploring the Vue.js documentation, I came across an example of iteratively rendering a Tree using the v-for directive. My aim was to modify the code to render multiple TreeItems in the App.vue file. I am puzzled by the fact that it is possible to i ...

Implementation of setProperties method on LineLayer in MapBox encounters resolution issues

I am currently utilizing the Android Mapbox SDK to integrate a VectorSource into a MapboxMap and then I am trying to incorporate a LineLayer onto the map as well. My version is 5.1.3 This code will be written in TypeScript due to its compatibility with t ...

A comprehensive guide on iterating through an array in JavaScript

Currently, I am facing an issue while trying to iterate over an array of Objects in React that have been fetched from Django. The object is stored as an array, but when attempting to map it, I encounter a typeerror stating "cannot read property map of unde ...

Differing preferences for indentation styles can lead to conflicting prett

My eslint setup is as follows: { "env": { "es2020": true, "jest": true }, "extends": [ "eslint:recommended", "plugin:react/recommended", "plugin:import/recommended&q ...

Error: The function Object.entries is not defined

Why does this error persist every time I attempt to start my Node.js/Express server? Does this issue relate to the latest ES7 standards? What requirements must be met in order to run an application utilizing these advanced functionalities? ...