Medium
You're debugging a React component that's causing memory leaks. Which implementation properly cleans up subscriptions?
1function DataStream({ source }) {2 const [data, setData] = useState([]);3 const [isActive, setIsActive] = useState(true);45 useEffect(() => {6 let subscription;78 if (isActive) {9 subscription = source.subscribe({10 next: (value) => {11 setData(prev => [...prev, value]);12 },13 error: (err) => console.error(err)14 });15 }1617 // Complete the cleanup18 return /* MISSING CODE */19 }, [source, isActive]);2021 return (22 <div>23 <button onClick={() => setIsActive(!isActive)}>24 {isActive ? 'Stop' : 'Start'}25 </button>26 <ul>{data.map((item, i) => <li key={i}>{item}</li>)}</ul>27 </div>28 );29}