How can we tailor a function in NextJS to display specific content according to the link provided?

In my /pages/index.js file, I have the following code snippet:

export default function Home() {
  return (
    <div className={styles.grid_container}>
      <NavBar/> 
      <div className={styles.center_pane}>
        <Overview/>
      </div>
      <div className={styles.right_pane}>
        <h2>Right Pane</h2>
      </div>
      <Footer/>
    </div> 
  )
}

While everything renders correctly, I'm looking for a way to parameterize the <Overview/> element so that it dynamically renders different components based on the user's navigation from the <Navbar>.

I've explored creating individual pages for each link, but it seems like an inefficient approach.

There was a similar question posted on Stack Overflow, but unfortunately, it went unanswered: Nextjs: render content based on sidebar choice

Any insights or suggestions would be greatly appreciated. Thank you.

Answer №1

To establish separate files for each page, consider creating individual files corresponding to each menu item. For example, if you have an item labeled "Overview" on the menu, you can create a pages/overview.js file. Next.js will automatically detect that visiting youraddress.com/overview should render the overview component.

Additionally, to encapsulate common logic surrounding your overview component, it's essential to develop a layout structure.

You can accomplish this by:

// components/navbar.js

const Navbar = () => {
  return <div>This content appears universally!</div>;
};

export default Navbar;

 // components/Layout.js
    
import Head from "next/head";

import Navbar from "./Navbar";

const Layout = ({ children }) => {
  return (
    <div>
      <Head>
        <title>Your amazing App</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <Navbar />
      {children}
    </div>
  );
};

export default Layout;
    
    
 // Embedding index.js within your layout
import Layout from "../components/Layout";
import styles from "../styles/Home.module.css";

export default function Home() {
  return (
    <Layout>
      <div className={styles.container}>This is the home page</div>
    </Layout>
  );
}

// pages/overview.js (the filename serves as the route, enabling access to localhost:3000/overview and automatic redirection in next.js)

import Layout from "../components/Layout";
import styles from "../styles/Home.module.css";

export default function overview() {
  return (
    <Layout>
      <div className={styles.container}>This is the overview page</div>
    </Layout>
  );
}

To view the complete source code, visit https://github.com/and-dutra/minimal-nextjs-layout

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

"Cross-origin resource sharing problem while working with NodeJs, Express, and React on

Currently, I am working on a small project where I am using NodeJs and Express for the backend and React for the client side. In order to tackle CORS policy issues, I have implemented the "cors" npm package on the server side, but unfortunately, it doesn& ...

Steps for implementing a Toggle Navigation Bar in CSS

I'm looking to implement a show/hide navigation menu similar to the one showcased in this inspiration source: Code Snippet (HTML) <div id="menus"> <nav id="nav"> <ul> <li><a href="#">HOME</a></li> <li& ...

Issues with functionality of React/NextJS audio player buttons arise following implementation of a state

I am currently customizing an Audio Player component in a NextJs application using the ReactAudioPlayer package. However, the standard Import Next/Audio and using just <Audio> without props did not yield the expected results. The player functions as ...

Concurrent execution within a Node.js function

I am facing a challenge with my function that deals with 1400+ crypto pairs. Each pair requires an API call and data storage, resulting in a significant amount of time for the entire process. The bottleneck occurs because each pair takes approximately 3-4 ...

HTML image identifier and canvas elements

Greetings! I have a question that I've been struggling to find an answer for on Google. I'm attempting to draw an image on a canvas and initially used the "new" constructor ( ballPic = new Image(); ballPic.src = "ball.png" ) which worked fine. Ho ...

Recursion functions seem to be not providing a return value

I wrote a recursive function that needs to return an object from an array of objects. Each object in the array includes a reference to a neighboring object, as shown below: { id: 5, neighbors: { north: 1, east: 6, south: 9, west: 4 ...

Supabase guidelines for utilizing getServerSideProps in Next.js

I am creating a Trello-like application using Next.js and Supabase as my backend as a service. Within my Supabase table, I have set up certain policies: https://i.sstatic.net/gl5Si.png The policies function correctly on the client-side with this code sn ...

Creating anonymous TypeScript class factories

Using TypeScript v2.2. In my codebase, there exists a class factory: export class A { name: string; } export function makeConstructor(name: string) { const newClass = class extends A { }; newClass.prototype.name = name; return newClass; } Howev ...

Is there a way to use SCTP with Socket.io and Node.js?

I have a new project in the works, creating a web application that will utilize web sockets to provide real-time updates for users. The plan is to seamlessly transmit changes from the back-end engine. My challenge lies in Node.js not supporting SCTP sock ...

Adding values to an array with the click of a submit button in React JS

I have a unique challenge with creating a form that includes custom inputs. const Input = (props) => { return ( <div> <label className={classes.label}>{props.label} <input className={classes.input} {... ...

After triggering an action, I am eager to make a selection from the store

To accomplish my task, I must first select from the store and verify if there is no data available. If no data is found, I need to dispatch an action and then re-select from the store once again. Here is the code snippet that I am currently using: t ...

AngularJS: Unable to preserve the data

I'm currently working on an issue with saving updated functions using angularJS. I've managed to edit the data and update it on the database side, but the changes aren't reflecting on the frontend side unless I logout and login again. I need ...

Issue encountered while retrieving information using Axios in a Vue.js application

Currently, I am in the process of developing a full stack application using Vue.js and Fastify.js (a Node framework). The Vue app is supposed to retrieve data from the API I created and display it in the browser console. However, I am encountering an issue ...

Interested in using jQuery to trigger the f12 key press?

In my current project, I have successfully disabled the f12 and right click using jQuery. Here is the code snippet: $(document).keydown(function(event){ if(event.keyCode==123){ return false; } else if(event.ctrlKey && event.shiftKey && ...

How can I utilize passed in parameters in Meteor React?

I am trying to figure out how to use two params that I have passed in the following example. Can someone please assist me? updater(layer, item){ this.setState({layer5: <img id="layer5" className="on-top img-responsive center-block" name="layer5" ...

Using a jquery function within a Laravel view

I am trying to retrieve a selected item from a dropdown menu using jQuery and then redirect it to a controller function. This function will return some data to be displayed based on the selected item. I could really use some assistance with this. Here is m ...

The useEffect React Hook is missing a dependency: 'fetchTracker'. Please make sure to add it to the dependency array or consider removing it

I recently encountered a challenging issue with one of my projects. The file in question is responsible for tracking data, which is then displayed on a map within an application. Despite my attempts to find a solution on StackOverflow, I have not found an ...

The navigation controller, responsible for updating navbar values, is only executed once

My goal is to construct a simple Angular application and familiarize myself with the basics. To start, I'm utilizing Yeoman's angular-generator for scaffolding. This generator comes with a predetermined .config featuring $routeProvider, which I ...

React Router V6 displays a white screen

I am experiencing an issue with React Router where it constantly displays a blank page even though I have implemented the code correctly. Here's how my code looks: App.js: import React from "react"; import { BrowserRouter as Router, Routes, ...

ng grid shift select issue mistakenly selects extra rows

Take a look at my plunker here: link var app = angular.module('app', []); // encountering a silly error that prevents me from pasting the entire code, please refer to the plunker for details To replicate the issue: Click on the first row with ...