Five Levels of the Magic Mushroom Experience

The Five Levels of the Magic Mushroom Psychedelic Experience Can Help You Prepare For Your First Experience Properly.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Creating a dynamic sidebar menu using react hooks.

It has been the regular convention for some front-end developers to create a separate view for each item in the sidebar menu. Hence creating a different route for all the sidebar menu items.

In this article, I will show you how to get the same result when creating a different route for each sidebar menu item with just one route.

Prerequisites

I will be using visual studio code(vscode) as my code editor in this tutorial.

Without further ado, let’s get started. Go to the terminal and run the following:

From the code snippet above, I created a directory in the desktop folder that holds the react template that was installed. Inside the new folder, some package was installed for the effective running of the application. Finally, I opened the directory in the VSCode.

Inside the SRC folder, I will create some new folders/files by running the following:

I have created three new folders inside the src folder, created a new file inside the view folder, created two new files in the component folder. and lastly created a container.js file inside the util folder.

Inside the containers.js file, add the following:

sidebar menu list

Going forward, I will import and render the sideBarMenu in the sidebar component as follows:

sidebar component

The sidebar menu items were mapped and rendered into an ordered list element.

Let’s handle what will happen when I click on any of the sidebar menu items in the following:

menu list click function

From the code snippet above, I added a function that routes not just to the root path but also added a query along with the path that holds the text inside each menu item that I clicked — The innerText was destructed from the argument. The handleItemClick function was called inside the opening tag of the list element as follows:

I will go to the Main component and add the following to it:

main component

A function component was created to handle what is going to be displaying depending on what item of the sidebar was clicked. A state was initialized, for now, the state is holding an empty array as its default value.

Remember, whenever I click on any of the sidebar menu items, it routes to the root path “/” and attach a query to it.

In the following, let’s see how you can get this query from the browser search bar from the Main component:

I accessed the search property of the browser location API to get the query that was passed to it from the sidebar click function above. A check was made to know if the found query params include a percent symbol with a number value, if yes, it will be replaced with a space. If not, it will return the actual query. The useEffect function was used to always get the query params once the search property of the location changes.

Now that I have gotten the query that lets me know which component to load in the Main component, I will create a switch statement to handle the rendering condition for it.

switch statement

I have used the switch statement to render different components depending on the query params — I will be creating the components soon. The query params value was capitalized, that was why I had to convert it to a lowercase value using the toLowerCase method in the switch statement.

Having done these, let’s go inside the component folder I created earlier and create the new component I had imported in the Main component by running the following in the terminal:

Inside the ViewOne component, let’s add the following:

view one component

Inside the ViewTwo component, let’s add the following to it:

view two-component

Moving forward, I will import the sidebar component and the Main component inside the dashboardView component I created earlier as follows:

dashboardView

Inside the app component, I will import the dashboard view component and pass it to the application route as follows:

app component

I imported some properties from the react-router-dom to enable me to route to different views, in my case is just one view.

With the demo, I guess I can call it a success. I can now change to different views depending on the sidebar menu items that are clicked. Also, when I reload the browser, the current view is retained just like when the views are added in their separate routes.

Tell me, How will you be rendering your sidebar menu item component going forward?

Kindly drop any comments or suggestions. If you have a topic based on the JavaScript/React.js ecosystem, feel free to reach out and I’d be glad to write on it. Thanks.

Add a comment

Related posts:

Circle of life

As a student of the life cycle — focused mostly on death and dying — I have been fascinated by my response to death among the animals on our farm. It’s only natural — say many. And they are only…

Gardening for the Health of It!

While planting a garden may seem so last season, it is not too late to enjoy homegrown (literally) produce that will be ready just in time for a fall harvest.

The best series of March 2023. Top 10 series of March. Mr. Marvel

Mr. Marvel has compiled the best TV series releases for March 2023. I can recommend the second season of the fantasy series “Shadow and Bone”, the Russian drama series about the life of actors…