I want to hook into two lifecycle events of a React component. When it's born, and when it dies. When it's born, I want to load some data from the URL or local storage, and that data should not change no matter how many times the internal state of the component changes.
At the end of the component's life, when it's disposed of, I want to store some data in the local storage.
I thought of using useMemo() for the birth event. And I considered using a callback in an empty useEffect for the death event.
Have I chosen them correctly?
P.S. I want this that I'm creating a general list component. I want to load the initial filters from the URL or the local storage once. And when things change, I want to update the URL. And when the list is closed I want to store the latest filtering state in the local storage.
Use
useEffectwith an empty array of dependencies ([]):React documentation for useEffect().
Notes:
useEffect(the callback function) is run once, after the component was mounted (attached to DOM)