What are the options for app directory routing and programmatic navigation in the upcoming 13 application

I am currently working on a project called Next 13 that involves using the app directory and MUI 5. The project's structure is organized as follows:

./src
./src/app
./src/app/dc
./src/app/dc/admin
./src/app/dc/admin/dc_types.jsx

However, when I try to navigate programmatically to the URL below, I keep encountering a 404 error:

http://localhost:3000/dc/admin/dc_types

The component responsible for the router.push() action looks like this:

"use client"

import { Box, Card, CardActionArea, CardContent, Typography } from "@mui/material"
import theme from "@/theme/theme"
import { usePathname, useRouter } from "next/navigation"

const ItemCard = (props) => {
    
    const defaultTitle = "Card Title"
    const defaultText = "Card Body"
    const defualtURL = "#"
    
    const { title, text, other } = props
    
    const cardTitle = title !== undefined ? title : defaultTitle
    const cardText = text !== undefined ? text : defaultText

    const router = useRouter()
    const pathname = usePathname()

    const cardId = cardTitle.replace(/\s/g, "_").toLowerCase()

    const handleClick = (e) => {
        console.log(`${pathname}/${cardId}`)
        router.push(`${pathname}/${cardId}`)
    }

    return (
        <Box>
            
            <Card elevation={2}>

                    <CardContent>
                    
                        <CardActionArea onClick={handleClick}>
                        
                            <Box sx={{ 
                                bgcolor: theme.palette.secondary.main,
                                color: theme.palette.common.white,
                                p: 1,
                                width: "100%",
                                "&:hover": {
                                    bgcolor: theme.palette.primary.main,
                                },
                            }}>

                            <Typography
                                id={cardId}
                                component={"typography"}
                                variant="h6"
                                sx={{
                                    color: theme.palette.common.white,
                                    display: "flex",
                                    justifyContent: "center",
                                    textDecoration: "none", 
                                }}
                            >
                                {cardTitle}
                            </Typography>

                            
                            </Box>

                        </CardActionArea>

                        <Typography component={"div"} variant="body2" sx={{ pt: 1 }}>
                            
                            { cardText }

                        </Typography>

                    </CardContent>

            </Card>

        </Box>
    )
}

export default ItemCard

Even though the value of ${pathname}/${cardId} is correctly showing as /dc/admin/dc_types, I am still unable to manually input the URL.

It seems like there might be some misconfiguration or misunderstanding on my end of how the app router functions. As someone who is new to Next.js, I would greatly appreciate any assistance in solving this issue.

Thank you in advance for your help!

Answer №1

When you are navigating the app directory, your paths are structured by folders. In order for a route to display a component, that particular file must be named page.tsx (or .jsx).

To make the necessary adjustment to your route:

./src/app/dc/admin/dc_types.jsx

you should update it to:

./src/app/dc/admin/dc_types/page.jsx

This will direct you to localhost:3000/dc/admin/dc_types/

I suggest taking a look at the routing section in the official Next.JS documentation to learn more about utilizing the app directory.

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 resolving state change issues in VUEX

I am facing an issue with making changes to the state while splicing an element from another array without affecting the state itself. To clarify, I want to remove one element from the array arrayWithFilters = [] without altering the state.selected.filte ...

Tips for ensuring a consistent highlight for an active link in Bootstrap

Does anyone have a solution for maintaining highlighted links on a web page? <div id="productnav"> <nav> <ul class="nav"> <li><a href="<?php echo $prefix; ?>pages/one.php?category=1" id="navelement1"<?php if ($c == 1) e ...

Tips to prevent redundancy in a one-line 'if' statement in JavaScript

While working on a piece of code, I came across something bothersome and couldn't find a solution even after doing some research. It's functional, but it feels redundant to have to type this.className three times: this.className != 'selecte ...

"Exploring the Power of Angular Change Detection with Promises in a Hybrid

We are currently in the process of upgrading an AngularJS project to Angular 7 by following the recommended "hybrid" approach where both frameworks coexist. However, we have encountered some issues with change detection when dealing with native promises. T ...

Trigger Element Upon Click

Forgive me in advance for the lack of quality in this question, but I'll proceed anyway: All I want is for an element to slide open when clicked with a mouse! That's all! More specifically, I am looking for a single menu item that, upon clickin ...

What is the best way to append data to the end of an object using ReactJS Hooks?

Looking to set up a checkbox page in ReactJS where data is filtered by checkboxes from different categories using ReactJS hooks. Currently, I am storing the selected checkboxes as an object: { color: 'orange', shape: 'square' } ...

Just for laughs: "The react-redux context value seems to be playing hide and seek; make sure to wrap the component in a <Provider> for it to show up!"

I encountered an issue while attempting to run a basic Jest test on a form React component: ● <LoginForm /> › clicking the validate button › should display page title ...

Building a TypeScript Rest API with efficient routing, controllers, and classes for seamless management

I have been working on transitioning a Node project to TypeScript using Express and CoreModel. In my original setup, the structure looked like this: to manage users accountRouter <- accountController <- User (Class) <- CoreModel (parent Class o ...

Populate the div with the URL parameter only when another span element is empty after a specified time interval using setTimeout

When displaying a person's name on a page, there are two different methods to consider. It can be extracted from the URL or retrieved from an email form in the CMS used. However, these two approaches sometimes conflict with each other. Currently, I h ...

Encountered difficulties sending JSON data to a REST endpoint using Node.js

Is there a way to bulk insert JSON data into MongoDB using Mongoose? I am aware of the insertMany method, but I'm encountering difficulties with extracting the correct req.body. Below is an image of my setup in Postman. https://i.sstatic.net/xFznd.pn ...

Guide to making a progress bar in Next.js

My goal is to have a Progress Bar displayed at the top of my website that fills up each time the page is loaded. I am currently working on implementing this feature in Next.js, but I am facing some challenges. Many available libraries for loading bars requ ...

Creating a hover effect for displaying image masks

My website features a profile page displaying the user's photo. I am attempting to create a functionality where when the user hovers over their photo, a transparent mask with text appears allowing them to update their profile picture via a modal. How ...

Struggling to effectively use XPath to target LI elements that contain specific text

Below is the HTML code for the list item in question: <li class="disabled-result" data-option-array-index="1" style="">4" (0)</li> Here is my attempt at using JavaScript to hide this list item, but it's not working as expected: var xpat ...

How to rename a class of an image tag using jQuery

I need to update the class name in my image tag <img src="img/nex2.jpeg" class="name"> When hovering over with jquery, I want to add a new class to it. After hovering, the tag should appear as follows: <img src="img/nex2.jpeg" class="name seco ...

Identical manipulator distinct infusion

I am looking to create multiple controllers that share the same logic, with only differences in injections. One way to achieve this is by creating controllers using a common function like so: var controllerFunc = function($scope, service) { $scope.se ...

Prompt the user to take an action by opening a modal or dialogue box from a dropdown menu

I am looking to implement a functionality where a modal or dialogue will be opened when a user selects an option from a dropdown menu. As the dropdown menu will have multiple options, different dialogues/modals should appear based on the selected option. ...

Building an anchor tag that employs the HTTP DELETE method within an Express.js app

Recently, I delved into using express.js with handlebars.js as my template engine. One task I wanted to tackle was creating a delete link that followed RESTful principles and used the HTTP DELETE verb instead of GET. After some trial and error, I discover ...

How can I adjust the column width in OfficeGen?

Currently, I am utilizing officeGen for the purpose of generating word documents. <sup> let table = [ [ { val: "TT", fontFamily: "Times New Roman", }, { val: "Ten hang", ...

Unable to suppress error in AngularJS $http.get() call when using catch() method

Below is a simplified version of the code I'm working with, running in CodePen for example: var app = angular.module("httptest", []); app.controller("getjson", ["$scope", "$http", function($scope, $http) { $http.get("https://codepen.io/anon/pen/L ...

Creating diverse content for various tabs using JavaScript

I have developed a code that uses a for loop to generate tabs based on user input. var tabs = ""; var y = 1; for (var x = 0; x < tabNum; x++) { tabs += "<li class = 'tabbers'>" + "<a href='#tab'>Tab</a>" + "& ...