Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Key" prop in map

Newbie here, Sorry for the silly question 🙂

How am gonna put the "Key" prop in this function?

The code is on the above of ContactShadows. I tried many ways but all the methods I tried are not working properly. Can you please check and tell me how am gonna put the "Key" props?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    import React, { Suspense, useEffect, useMemo, useRef, useState } from "react";

import "./PartOne.css";

const PartOne = () => {
const [rooms, setRooms] = useState([
    Room1,
    Room2,
    Room3,
    Room4,
    Room5,
    Room6,
    Room7,
    Room8
]);

useEffect(() => {
    console.log("rooms state:", rooms);
}, [rooms]);

return (
    <div className="wrapper">
        {rooms.map((room, roomIndex) =>
            room({ position: positions[roomIndex] })
        )}
    </div>
);
};
export default PartOne;

Thank You

>Solution :

You could wrap that element from room function in a <React.Fragment> and assign the key on it:

return (
    <div className="wrapper">
        {rooms.map((room, roomIndex) => (
            <React.Fragment key={`${roomIndex}`}>
                {room({ position: positions[roomIndex] })}
            </React.Fragment>
        ))}
    </div>
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading