setProfiles({
...profiles,
idx: { ...idx, tag: "hey" },
});
I am making a copy of profiles object, I want to replace the idx object with a copy of idx and tag appended to it. Where did I go wrong? I am using React Hooks btw
Here is my whole code. I'm not sure why your suggestions aren't working. I must've messed up the format somehow. I would appreciate it if someone could help me out on this, I have been stuck on this for about an hour right now and it's just irritating me. I found another way to do it but it's not so efficient.
const ProfileCard = ({ profiles, setProfiles }) => {
const [isCardExpanded, setIsCardExpanded] = useState(false);
// Handles the logic of adding a tag
const onKeyDownHandler = (e, idx) => {
let updatedProfile = profiles[idx];
if (e.keyCode === 13) {
setProfiles({
...profiles,
idx: { ...profiles.idx, tag: "hey" },
});
}
};
return Object.keys(profiles).map((idx) => {
return (
<div className="profile-container" key={profiles[idx].id}>
<div className="profile-img-and-content">
<div className="profile-img">
<img
src={profiles[idx].pic}
alt={`${profiles[idx].firstName} ${profiles[idx]}'s Profile`}
/>
</div>
<div className="profile-content">
<h1>
{`${profiles[idx].firstName.toUpperCase()} ${profiles[
idx
].lastName.toUpperCase()}`}
</h1>
<div className="profile-content-no-header">
<p>{`Email: ${profiles[idx].email}`}</p>
<p>{`Company: ${profiles[idx].company}`}</p>
<p>{`Skill: ${profiles[idx].skill}`}</p>
<p>{`Average: ${average(profiles[idx].grades)}%`}</p>
<div className={`${!isCardExpanded ? null : "ghost"}`}>
<div className="tags">tag 1</div>
</div>
<input
onKeyDown={(e) => {
onKeyDownHandler(e, idx);
}}
className={`input-tag ${!isCardExpanded ? null : "ghost"}`}
type="text"
placeholder="Add a tag"
/>
<div
className={`${isCardExpanded ? null : "ghost"}`}
style={{ marginTop: "40px" }}
>
{renderGrades(profiles[idx].grades)}
</div>
<div className={`${isCardExpanded ? null : "ghost"}`}>
<div className="tags">h</div>
</div>
<input
className={`input-tag ${isCardExpanded ? null : "ghost"}`}
type="text"
placeholder="Add a tag"
/>
</div>
</div>
</div>
<button onClick={expandCard}>{isCardExpanded ? "-" : "+"}</button>
</div>
);
});
};
return <div>{renderProfiles(profiles)}</div>;
};