简体   繁体   English

React - 删除功能不起作用。 uuid() 不生成 ID

[英]React - Delete Functionality is not working. uuid() is not generating IDs

Please help me, Delete Icon is not functional, when I click on delete icon it delete all the contact, on refreshing.请帮助我,删除图标不起作用,当我点击删除图标时,它会删除所有联系人,刷新时。 it returns all the previous contacts.它返回所有以前的联系人。 I am also using localStorage.我也在使用本地存储。

I have added all the Component of the React App Project.我已经添加了 React App 项目的所有组件。

App.js应用程序.js

import { v4 as uuid } from "uuid";

const App = () => {
  
  const LOCAL_STORAGE_KEY = "contacts";
  const [contacts, setContacts] = useState([]);

  const addContactHandler = (contact) => {
    console.log(contact);
    setContacts([...contacts, { id: uuid(), ...contact }]);
  };

  const removeContactHandler = (id) => {
    const newContactList = contacts.filter((contact) => {
      return contact.id !== id;
    });
    setContacts(newContactList);
  };

 
  useEffect(() => {
    const retrieveContacts = JSON.parse(
      localStorage.getItem(LOCAL_STORAGE_KEY)
    );
    if (retrieveContacts) {
      setContacts(retrieveContacts);
    }
  }, []);


  useEffect(() => {
    if (contacts.length) {
      localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(contacts));
    }
  }, [contacts]);

  return (
    <>
      <div className="app">
        <Header />
        <AddContact addContactHandler={addContactHandler} />
        <ContactList contacts={contacts} getContactId={removeContactHandler} />
      </div>
    </>
  );
};

export default App;

ContactList.js联系人列表.js

const ContactList = (props) => {
  const deleteContactHandler = (id) => {
    props.getContactId(id);
  };

  const renderContactList = props.contacts.map((contact) => {
    return (
      <>
        <ContactCard
          contact={contact}
          clickHandler={deleteContactHandler}
          key={contact.id}
        />
      </>
    );
  });

  return (
    <>
      <div className="contactList">
        <h2 className="contactList__title">Contact List</h2>
        <div className="contactList__container">
          {renderContactList}
        </div>
      </div>
    </>
  );
};

ContactCard.js ContactCard.js

const ContactCard = (props) => {
  const { id, name, email } = props.contact;

  return (
    <>
      <div className="contactCard">
        <div className="contactCard__contact">
          <img
            className="contactCard__userIcon"
            src={userIcon}
            alt="user-icon"
          />
          <div className="contactCard__userName">          
            <h2>{name}</h2>       
            <p>{email}</p>
          </div>
        </div>
        <div className="contactCard__delIcon">
          <img
            src={delIcon}
            alt="del-icon"
            onClick={() => props.clickHandler(id)}
          />
        </div>
      </div>
    </>
  );
};

export default ContactCard;

I have researched out the references.我已经研究了参考资料。 Unable to get the Solution.无法获得解决方案。

The effect to store the contacts do not save empty arrays. Thats why you get the old array after refreshing your page.存储contactseffect不保存空 arrays。这就是为什么刷新页面后得到旧数组的原因。

Just remove the condition.只需删除条件。

useEffect(() => {
    localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(contacts));
  }, [contacts]);

But you should consider to remove this effect.但是你应该考虑消除这种影响。 Save the contacts directly after setting the state instead.改为设置state后直接保存联系人。

const addContactHandler = (contact) => {
    console.log(contact);
    const newContactList = [...contacts, { id: uuid(), ...contact }];
    setContacts(newContactList);
    localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(newContactList));
  };

  const removeContactHandler = (id) => {
    const newContactList = contacts.filter((contact) => {
      return contact.id !== id;
    });
    setContacts(newContactList);
    localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(newContactList));
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM