Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Crear rutas dinámicas usando Next.js y React para hacer links con parámetros

Tiempo de lectura: < 1 minuto

Hoy vamos a aprender cómo podemos crear rutas dinámicas para realizar links con parámetros utilizando Next.js.

Lo primero que vamos a hacer es crear una carpeta dentro de nuestras páginas llamada article y dentro vamos a crear un archivo llamado:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[id].tsx
[id].tsx
[id].tsx

Quedando:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
-pages
-article
- [id].tsx
-pages -article - [id].tsx
-pages
   -article
      - [id].tsx

Esto nos permitirá poder invocar la URL:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
http://localhost:3000/article/1
http://localhost:3000/article/1
http://localhost:3000/article/1

Ahora dentro de la página podemos crear:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React from 'react';
import { useRouter } from 'next/router';
const ArticlePage: React.FC<ArticlePageProps> = ({}) => {
const router = useRouter();
const { id } = router.query;
return (
<div>
<h1>{id}</h1>
</div>
);
};
export default ArticlePage;
import React from 'react'; import { useRouter } from 'next/router'; const ArticlePage: React.FC<ArticlePageProps> = ({}) => { const router = useRouter(); const { id } = router.query; return ( <div> <h1>{id}</h1> </div> ); }; export default ArticlePage;
import React from 'react';
import { useRouter } from 'next/router';

const ArticlePage: React.FC<ArticlePageProps> = ({}) => {
  const router = useRouter();
  const { id } = router.query;

  return (
    <div>
      <h1>{id}</h1>
    </div>
  );
};

export default ArticlePage;

Para invocarlo tenemos que crear un Link de esta forma:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<Link href={`/article/1`}>Mi link</Link>
<Link href={`/article/1`}>Mi link</Link>
<Link href={`/article/1`}>Mi link</Link>

Si queremos abrir una página con dos paths tendremos que crear una carpeta con el path de la siguiente forma:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
-pages
-article
- [id]
-[name].tsx
-pages -article - [id] -[name].tsx
-pages
   -article
      - [id]
         -[name].tsx

Y el código quedaría así:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React from 'react';
import { useRouter } from 'next/router';
const ArticlePage: React.FC<ArticlePageProps> = ({}) => {
const router = useRouter();
const { id, name } = router.query;
return (
<div>
<h1>{id}</h1>
<p>{name}</p>
</div>
);
};
export default ArticlePage;
import React from 'react'; import { useRouter } from 'next/router'; const ArticlePage: React.FC<ArticlePageProps> = ({}) => { const router = useRouter(); const { id, name } = router.query; return ( <div> <h1>{id}</h1> <p>{name}</p> </div> ); }; export default ArticlePage;
import React from 'react';
import { useRouter } from 'next/router';

const ArticlePage: React.FC<ArticlePageProps> = ({}) => {
  const router = useRouter();
  const { id, name } = router.query;

  return (
    <div>
      <h1>{id}</h1>
      <p>{name}</p>
    </div>
  );
};

export default ArticlePage;

Y para invocarlo:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
http://localhost:3000/article/1/Article Name
http://localhost:3000/article/1/Article Name
http://localhost:3000/article/1/Article Name

O con un link:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<Link href={`/articles/1/${encodeURIComponent("titulo")}`}>Mi artículo</Link>
<Link href={`/articles/1/${encodeURIComponent("titulo")}`}>Mi artículo</Link>
<Link href={`/articles/1/${encodeURIComponent("titulo")}`}>Mi artículo</Link>
0

Deja un comentario