React page leave operation

I have a nextjs application. There is a page that has a list of documents. If someone uploads a document, the list gets one item added. The task is, if someone moves out of the page and if the list still have some items, we need to call one API for each documents. How can it do it in react and nextjs?

export const Page = () => {
  const [documents, setDocuments] = useState([]);

  const deleteDocuments = useCallback(() => {
    documents.forEach(d => deleteFunction)
  }, [deleteFunction, documents]);

  useEffect(() => {
    return () => deleteDocuments()
  }, [deleteDocuments] );

  return "...some..html.."
}

We are using useEffect‘s cleanup function to handle that.

But, the issue is, since the delete function is used, it is part of useEffect dependency and since that function depends on documents, it is part of dependency function. Due to this, anytime user adds a document inside documents array, the useEffect’s cleanup function is called because array is changed.

I want this function to be called only once on page leave. (it is okay if it is being called two times initially for react strict mode)

There are two ways we potentially can fix

First

We can omit the dependency for delete function

const deleteDocuments = useCallback(() => {
    documents.forEach(d => deleteFunction)
  }, [deleteFunction]); // <-- remove documents from the list

  useEffect(() => {
    return () => deleteDocuments()
  }, [deleteDocuments] );

I am not a fan of this approach because we never know when this thing starts failing because of unhandled dependency

Second

To use useRef

const [documents, setDocuments] = useState([]);
const documentsRef = useRef(documents);

useEffect(() => {
    documentsRef.current = documents;
  }, [documents]);

const deleteDocuments = useCallback(() => {
    documentsRef.current.forEach(d => deleteFunction)
  }, [deleteFunction]); // <- no need to have documents or documentRef as dependency

  useEffect(() => {
    return () => deleteDocuments()
  }, [deleteDocuments] );

If we maintain the documentRef, we don’t need to maintain that in the dependency array.

However, I am not sure if this is the best approach. Because React suggest to not to fall for the pitfall of using useRef. However, the example they suggested make sense for them because it would connect and close connection but our example is not prone to that bug because as soon as something is rendered, the document array would be [] and it does not matter if the delete document function is called two time for an empty array.

Anything better?

Is there a better solution to tackle this?

PS

I understand that, I haven’t added window.beforeunload event. I ommitted that for simplicity in example, but I am planning to have it in actual production code.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật