Key issue having 4 different divs for screen size
Unanswered
Asian swamp eel posted this in #help-forum
Asian swamp eelOP
cpuldnt fix key issue can anyone help me ?
42 Replies
Asian swamp eelOP
ayone here ?
Vespid wasp
In your UserCard component. Do you map over some array of data?
If this is the case, take a look at the following from the React documentation: https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key
Asian swamp eelOP
my question is related that i am using 4 divs different of screen size and keys are same as i write but issue is why it giving it ? i try puting keys but issue isnt going :/
@Asian swamp eel my question is related that i am using 4 divs different of screen size and keys are same as i write but issue is why it giving it ? i try puting keys but issue isnt going :/
hey, the map function also has a automatically generated key as callback
try using it
{data.map((row, key) => (
<div key={key} className="border border-black p-4 rounded-lg shadow-md mb-4 w-[80%] mx-auto">
<div className="flex justify-between">
<span className="font-semibold">Name:</span>
<span>{row.name}</span>
</div>
<div className="flex justify-between mt-2">
<span className="font-semibold">Email:</span>
<span>{row.email}</span>
</div>
<div className="flex justify-between mt-2">
<span className="font-semibold">Telephone:</span>
<span>{row.telephone}</span>
</div>
<div className="flex justify-center mt-4 space-x-2">
<button className="bg-blue-500 text-white px-4 py-2 rounded">View</button>
<button className="bg-green-500 text-white px-4 py-2 rounded">Edit</button>
<button className="bg-red-500 text-white px-4 py-2 rounded" >
Delete
</button>
</div>
</div>
))}key is automatically generated by reactAsian swamp eelOP
but what about if i want to delte specific id and backend made like this ?
js
import { NextResponse } from "next/server";
// In-memory data storage for user data
let API: { user: number; name: string; email: string; phone: string }[] = [];
let userIdCounter = 1;
/**
* Handles POST request to add new user data.
* Expects name, email, and phone in the request body.
* Adds the new user to the API array.
*/
export async function POST(request: Request) {
const data = await request.json(); // Parse the request body
// Validate the data types for name, email, and phone
if (
typeof data.name === "string" &&
typeof data.email === "string" &&
typeof data.phone === "string"
) {
// Create a new user object
const newUser = {
user: userIdCounter++, // Assign a unique user ID
name: data.name,
email: data.email,
phone: data.phone,
};
// Add the new user to the API array (in-memory database)
API.push(newUser);
// Return success response
return NextResponse.json({ message: "User added successfully" });
} else {
// If data types are invalid, return an error response
return NextResponse.json(
{
message: "Invalid data type. Please provide valid name, email, and phone.",
},
{ status: 400 } // Bad request status code
);
}
}useridcounter++ ?
Asian swamp eelOP
then plese help me little so i can try to avoid mistake again ?
first, In-memory doesnt exist in nextjs
so
u probably need to rework your api so u can use it with a database
Asian swamp eelOP
i make dummy api for pratice as i am making crud project iin next.js
ok...
what exactly do u want help in?
Asian swamp eelOP
i am trying that should counter use on frontend side or backend ?
backend
u can generate a uuid on your backend as id
Asian swamp eelOP
mean uuid is different from all ? and dont need counter ?
@Asian swamp eel mean uuid is different from all ? and dont need counter ?
no you dont need a counter
since this object oriented its probably mongodb right?
for mongodb i always use the crypto api from node
to generate uuid
u dont need to do that btw
u can use any random string or number as id
in mongodb for example...
Asian swamp eelOP
will try to do ... once done then tell you !!! thank for help bro !!!
Asian swamp eelOP
is node.js based in next.js ?
or next.js backend is differnt from node ?
yes nextjs uses node as backend
Asian swamp eelOP
ok
but it also uses the edge runtime for the middleware for example
its basically like node but many things removed for perfomance
Asian swamp eelOP
hmm