Steps to efficiently enumerate the array of parameters in the NextJS router:

In my NextJS application, I have implemented a catch all route that uses the following code:

import { useRouter} from 'next/router'

This code snippet retrieves all the parameters from the URL path:

const { params = [] } = router.query

When I console log the params value, I can see all the URL path elements.

My goal is to display all the params values in an unordered list:

return <ul>
  {params.map((param) => {
    <li>{param}</li>
  })}
</ul>

However, when I try this code, nothing gets displayed. The list is empty.

How can I modify this to successfully display the list of parameters?

Answer №1

When using curly braces in JavaScript, remember to include the return keyword.

return <ul>
  {params.map((param) => {
    return (<li>{param}</li>);
  })}
</ul>

This is an example of how arrow functions behave in JS.

The initial code does not have a return statement, resulting in undefined being returned and nothing being displayed:

return <ul>
  {params.map((param) => {
    <li>{param}</li>
    //No return statement  
})}
</ul>

In short: You can eliminate the curly braces.

return <ul>
  {params.map((param) => <li>{param}</li>)}
</ul>

Answer №2

It seems like there may be an issue with how you are using the .map function. Consider trying this approach instead:

return params.map((param) => param);

By implementing this, your params will be displayed together on a single line. Feel free to customize the formatting based on your specific preferences.

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

ID could not be retrieved from the checkbox

I am facing an issue with my HTML checkboxes. The ids are generated from an angular ng-repeat, but when I try to collect the id for use, it always returns as undefined. $("input:checkbox.time-check-input").each(function () { var ruleUnformatted = ""; ...

Custom Component in React Bootstrap with Overflowing Column

I am working on a custom toggle dropdown feature in my React application: import React from 'react'; import 'react-datepicker/dist/react-datepicker.css'; const DateRange = props => ( <div className="dropdown artesianDropdo ...

Utilizing Filepond to extract base64 data in a React JS environment

Struggling to extract a base64 from an image upload and send it along with other values to the server. Due to API limitations allowing only 2 files at a time, I'm unable to process uploads individually. Having difficulty in mapping the 'data&apo ...

What is the best way to create a unique page transition animation using Framer Motion and Next.js?

I am currently using Next.js, React, TypeScript, and Framer Motion in my project. I have successfully implemented page transitions from right to left when changing pages within my _app.tsx file. However, I am facing an issue with the arrow navigation; both ...

Tips for associating an id with PrimeNg menu command

Within my table, I have a list of items that I would like to enhance using PrimeNg Menu for dropdown menu options. The goal is to enable navigation to other pages based on the selected item id. When a user clicks on a menu item, I want to bind the id of th ...

Is it possible to capture a submit event from a form within an iframe using jQuery or JavaScript

If I have a webpage with an embedded iframe containing a form, how can I update a hidden field value on the main page once the form is submitted? What is the best way to trigger an event in the parent page upon form submission? Here's a simplified ex ...

Issues arising from TypeScript error regarding the absence of a property on an object

Having a STEPS_CONFIG object that contains various steps with different properties, including defaultValues, I encountered an issue while trying to access the defaultValues property from the currentStep object in TypeScript. The error message indicated tha ...

Steps to customize a CSS file within node_modules

Is there a way to make changes to a CSS file in the "node_modules" dependency without them being overwritten when I run npm install? I want to keep the modifications I've made to the node module files. ...

Delay in Ionic list item navigation

As I continue to build my app using "$ ionic start myApp sidemenu" and deploy it directly to Android, I've noticed a significant delay when tapping on the playlist page, such as when selecting "Indie". It feels like there is a 300ms lag before the pag ...

Incorporating asynchronous file uploads to a server using a for loop

I am in the process of transforming my original code into "async" code. The initial code queries the database and retrieves the results, which includes images. I want to optimize writing the images asynchronously to my nodejs server as the current synchro ...

What is the best way to generate an array of dictionaries using Python?

I have a dictionary structured like this: {'A':0,'C':0,'G':0,'T':0} The objective is to generate an array containing multiple dictionaries with the same structure, for example: [{'A':0,'C':0,&a ...

dissecting mongo queries using nodes

I am thinking about organizing my mongo db queries into a separate js file to make it easier to reuse the code. I have tried the format below but it doesn't seem to work. Does anyone have any suggestions on how I could accomplish this? queries.js va ...

Conceal descendant of list item and reveal upon clicking

In my responsive side menu, there is a submenu structured like this: .navbar ul li ul I would like the child menus to be hidden and only shown when the parent menu is clicked. Although I attempted to achieve this with the following code, it was unsucces ...

Different Levels of Dynamic Routing in Next.js

I am a beginner learning about next.js. I'm curious to know how I can implement two levels of dynamic routing in next.js? The URL structure I want to achieve is http://localhost:3000/company/[slug1]/[slug2] After going through the official documenta ...

Fixing the issue: "Tricky situation with JavaScript not working within Bootstrap 4's div tag while JS functions properly elsewhere"

Currently, I'm working on implementing a hide/show function for comments using JavaScript. Fortunately, I was able to find a helpful solution here (thanks to "PiggyPlex" for providing the solution on How can I hide/show a div when a button is clicked? ...

Can a jQuery object be generated from any random HTML string? For example, is it possible to create a new link variable like this: var $newlink = $('<a>new link</a>')?

I'm looking to create an object without it being attached to the dom. By passing a HTML string, I want to insert the element into the dom and still have a reference to it. ...

MS Edge modifies the attribute's empty value to 1

I have written a JavaScript code to extract values from a list, but in the Windows Edge browser, it returns a value of 1 even when the actual value of the <li> tag is blank. For example: HTML Code <ul> <li value="">Test 1</li&g ...

Positioning a designated div directly above a designated spot on the canvas

I'm grappling with a challenge in my game where the canvas handles most of the animation, but the timer for the game resides outside the canvas in a separate div. I've been struggling to center the timer around the same focal point as the squares ...

Unreliable static URLs with Next.js static site generation

I've recently built a Next.js website with the following structure: - pages - articles - [slug].js - index.js - components - nav.js Within nav.js, I have set up routing for all links using next/link, including in pages/articles/[slug].j ...

Adding my 'no' or 'id' in a URL using a JavaScript function can be accomplished by creating an onClick event

Here is the function I'm working on: function swipe2() { window.open('edit.php?no=','newwindow') } This is part of my PHP code (I skipped some lines): for ($i = $start; $i < $end; $i++) { if ($i == $total_results) { ...