Not working with Dynamic Routing
See original GitHub issueWrapping the component in _app.js with the page transition component throws an error once you try to navigate anywhere from a post using dynamic routing. Using the most basic form from the example without any styling.
import React from 'react';
import App, { Container } from 'next/app';
import { PageTransition } from 'next-page-transitions';
class MyApp extends App {
// Only uncomment this method if you have blocking data requirements for
// every single page in your application. This disables the ability to
// perform automatic static optimization, causing every page in your app to
// be server-side rendered.
// static async getInitialProps({ Component, ctx }) {
// let pageProps = {}
// console.log('hello');
//
// if (Component.getInitialProps) {
// pageProps = await Component.getInitialProps(ctx)
// }
//
// return { pageProps }
// }
render() {
const { Component, pageProps, router } = this.props;
console.log('_app', router);
return (
<Container>
<PageTransition timeout={300} classNames="page-transition">
<Component {...pageProps} key={ router.route }/>
</PageTransition>
</Container>
);
}
}
export default MyApp;
Here’s an example of my blog page using the dynamic routing links
import Link from 'next/link';
function BlogPosts(props) {
const blogImg = {
backgroundImage: `url("${props.imgSrc}")`
};
if(props.homePage) {
return(
<React.Fragment>
<div className ="blog-post-wrapper">
<div className ="blog-pic" style = {blogImg}></div>
<div className ="blog-text-container">
<h3>{props.postTitle}</h3>
<p>{props.postPreview}</p>
<Link href = '/blog/[posts]' as = {`/blog/${props.postSlug}`}><a className ="blue-button blog-button">READ MORE</a></Link>
</div>
</div>
</React.Fragment>
);
} else {
return(
<React.Fragment>
<Link href = '/blog/[posts]' as = {`/blog/${props.postSlug}`}>
<div className ="blog-post-wrapper-page">
<div className ="blog-pic" style={blogImg}></div>
<div className ="blog-text-container">
<div className ="blog-title-container">
<h3>{props.postTitle}</h3>
<p>{props.postPreview}</p>
</div>
<div className ="blog-date-container">
<p className = 'blog-date'>{props.date}</p>
</div>
</div>
</div>
</Link>
</React.Fragment>
);
}
}
export default BlogPosts
and the actual page itself
import { useRouter } from 'next/router';
import postsData from '../../data/PostsData.js';
import Navbar from '../../components/Navbar.js';
import '../../style.sass';
function BlogPost(props) {
const router = useRouter();
const postData = postsData.find((data) => {return data.slug === router.query.posts});
const mainImg = {
backgroundImage: `url("${postData.imgSrc}")`
};
return (
<React.Fragment>
<Navbar postPage = {true}/>
<div className = 'main-post-image' style = {mainImg} ></div>
<h1></h1>
<p></p>
</React.Fragment>
);
}
export default BlogPost
I’m using next 9.0.3
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Dynamic routing is not working while adding a new post
While adding a new vechile dynamic route showing loading. It is not fetching the new data from state. App.js: import React, { Component...
Read more >Dynamic Routing Not Working Correctly? - Cisco Community
Solved: Hello, I'm studying for the CCNA, and trying to figure my head around dynamic routing. The task is to set up 3x...
Read more >Routing in Next.js – How to Set Up Dynamic Routing with Pre ...
In this tutorial, you'll learn how to set up dynamic routing in Next.js. You'll also learn about pre-rendering and why it's important.
Read more >Dynamic route is not working with index.js · Issue #123 - GitHub
Describe the problem: Dynamic routing is not able to fetch params with [slug]/index.js. This is what I am doing so far export async...
Read more >Error message: Dynamic routing is not enabled in ... - AskF5
This message is returned when attempting to enter the imish shell if a dynamic routing protocol is not enabled for a route domain,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have same problem.
@alenart91 Did you solve this problem?
So my issue was I had to use the query object along with getInitialProps in the dynamic pages. I was using the router to compare the data and return the proper object for specific posts. When I would navigate from that page the variable I stored it in would return undefined because the query object would be empty. Still not 100% sure why it calls everything again on that page when navigating away from it however.