Skip to content

Latest commit

 

History

History
22 lines (18 loc) · 443 Bytes

destroy-hook.md

File metadata and controls

22 lines (18 loc) · 443 Bytes

How to run a clean up function on component destroy

If your effect returns a function, React will run it when it is time to clean up:

useEffect(() => {
  chart.current = createChart('chart-container', {
    width: 960,
    height: 500,
    crosshair: {
      mode: CrosshairMode.Normal,
    }
  });

  return function () {
    chart.current?.remove();
  }
}, []);

References: