如何在不影响其他用户配置文件的情况下将数据添加到用户配置文件 React

how to add data to a user's profile while not affecting other users' profile React

点击用户后,会转到 his/her 个人资料页面。单击个人资料页面上的注释标签,用户可以将消息添加到 his/her 个个人资料。

你那里的代码非常复杂,但我试图理解你的观点......最后哦 好的,所以每个病人都必须有自己的笔记,你必须首先将患者 ID 添加到每个笔记对象,这样做是通过获取 id 参数(你已经这样做了,并将其传递给 AddNote 组件:

export default function EditPatient({//...})
  //.....
const { id } = useParams();
 //.....
<AddNote addNote={handleAddNote} idPatient={id} />

现在,继续您的 AddNote 组件,我们需要将患者 ID 添加到备注对象,就像这样:

export default function AddNote({ addNote, idPatient }) {
/...
const newNote = {
          userPic: <AccountBoxIcon color="primary" fontSize="large" />,
          user: "Test User",
          time: formatAMPM(new Date()),
          note: note,
          id: generateId(),
          isFinal: false,
          idPatient: idPatient //<----add it here
        };

现在,剩下要做的是根据 idPatient 过滤每个患者资料的注释,返回到 EditPatient 组件:

export default function EditPatient({
   //....
const { id } = useParams();
notes = notes.filter((note)=> note.idPatient === id)

PS:当你做得很好的时候,你正在让你的代码变得如此复杂和不可读,所以我的建议是使用像 Context api 或 redux 这样的状态管理工具来让你的代码更清晰并且易于维护而不是顶级 useState()