#Key issue having 4 different divs for screen size
43 messages ยท Page 1 of 1 (latest)
๐ This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
๐ต๏ธ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
โ
You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
ayone here ?
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
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 react
but what about if i want to delte specific id and backend made like this ?
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++ ?
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
i make dummy api for pratice as i am making crud project iin next.js
i am trying that should counter use on frontend side or backend ?
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...
will try to do ... once done then tell you !!! thank for help bro !!!
no problem :(
:)*
yes nextjs uses node as backend
ok