mercredi 31 mars 2021

For loops and if statements in Hooks?

Is there a way where I can use for loops and if statements without breaking the hook rule? To elaborate, I am currently trying to compare two lists (allData and currentSelection) and if there are similarities, I will add them to another list (favData). However, I am constantly either having visibility issues or errors. If I can get some help, I would much appreciate it!

const [favData, setFavData] = useState([]);    
useEffect(() => {     
    getFilterFavMeal();   
}, []);    
function getFilterFavMeal() {        
    allData.forEach((mealList) => {            
        currentSelection.forEach((mealList2) => {                
            if (mealList["menu_item"]["menu_item_id"] === mealList2.value) {                             
            // with push, I have visibility issues 
//                       favData.push(mealList);                       
                setFavData(mealList);                
            }            
        });        
    });        
    setFavData(favData);    
} 

Aucun commentaire:

Enregistrer un commentaire