I'm trying to display modal when no products have been selected by user before. I ended up having an infinite loop of useEffect() dependency. I'm not sure how to do it correctly in React.
import React, { useState, useEffect, useCallback } from 'react';
const MyComponent = ({ products }) => {
    const [modals, setModals] = useState({});
    const [currentModalName, setCurrentModalName] = useState('');
    const setCurrentModal = useCallback(
        (modalName, data = {}) => {
            if (modalName) {
                setModals({
                    ...modals,
                    [modalName]: {
                        ...modals[modalName],
                        ...data
                    }
                });
            }
            setCurrentModalName(modalName);
        },
        [modals]
    );
    useEffect(
        () => {
            if (!products.length) {
                setCurrentModal('chooseProduct')
            }
        },
        [products, setCurrentModal] // setCurrentModal causes infinite loop
    );
    return (
        <div>...</div>
    );
}
export default MyComponent;
I can just remove setCurrentModal from the dependencies, but I'm warned about it. If I add it, my React app freezes.
How can I organize my code to avoid freezing?
 
     
    