3
Guys, I have a question on Next JS, I have a page with name listid.js
, when accessing for example http://localhost:3000/listid/10 I would like to receive id 10 as parameter on my page listid.js
:
import React, { useState, useEffect } from 'react';
// components
import ListUser from "components/ListUser.js";
function ListId() {
return (
<>
<div className="flex flex-wrap mt-4">
<div className="w-full mb-12 xl:mb-0 px-4">
<ListUser />
</div>
</div>
</>
);
}
export default ListId;
Follow the code of my compound ListUser
:
import React, { useState, useEffect } from 'react';
import Api from "../../services/api";
import Router from 'next/router';
import Link from "next/link";
function ListUser() {
const [user, setUser] = useState(null);
useEffect(() => {
async function getItems() {
try {
const { data } = await Api.get("/user"); //preciso passar o parametro aqui
setUser(data);
} catch (error) {
console.log(error);
}
}
getItems();
}, []);
// console.log(users.length);
if (users === null) {
return <h2>Carregando...</h2>;
}
return (
<>
<div>
{
user?.map((user) => {
return (
<p>{user.id}</p>
)})
}
</div>
</>
);
}
export default ListUser;
How can I get the id in the url and pass the api request?
Thank you very much Gabriel, your explanation was of great help, I managed to do here!
– Wendell