简体   繁体   English

从另一个组件传入道具后,组件中的功能不起作用

[英]Function in component not working after passing in props from another component

I have a working function when I pass an array of data inside the same component.当我在同一个组件内传递数据数组时,我有一个工作函数。
This function makes an array of all possible combinations of ingredients, then makes API calls to find all possible drinks, and then filters through to find the ones that only contain the ingredients in the array.此函数将所有可能的成分组合组成一个数组,然后调用 API 以查找所有可能的饮料,然后过滤以查找仅包含数组中成分的饮料。
However, when I try to pass the array of data as props from another component, the function stops working half way through...但是,当我尝试将数据数组作为来自另一个组件的道具传递时,该函数在中途停止工作......
The props are received fine and the function starts but then it stops working on line 64-72.道具接收良好,功能启动,但随后在第 64-72 行停止工作。 The valid results are not getting pushed to my result array.有效结果没有被推送到我的结果数组。
The two images show function passed with props and hard coded array: Any help would be appreciated!!这两张图片显示了通过道具和硬编码数组传递的函数:任何帮助将不胜感激! Component code:组件代码:

import React from 'react'
import { useEffect, useState } from 'react'
import axios from 'axios';
import './style.css'

function MultiSearch({clickState}) {
    //console.log(ingredients)
    console.log(clickState)

const [filtResult, setFiltResult] = useState([])
const ingredients = ['Vodka', 'Orange juice']
//console.log(ingredients)
// use usestate to add salt and other ingredients like that as default. Then add user ingredients onto existing array

function getCombinations(valuesArray) {
    let combi = [];
    let temp = [];
    let slent = Math.pow(2, valuesArray.length);
    for (let i = 0; i < slent; i++) {
        temp = [];
        for (let j = 0; j < valuesArray.length; j++) {
            if ((i & Math.pow(2, j))) {
                temp.push(valuesArray[j]);
            }
        }
        if (temp.length > 0) {
            combi.push(temp);
        }
    }
    combi.sort((a, b) => a.length - b.length);
    // console.log(combi.join("\n"));
    console.log(combi.filter(x => x.length > 1))
    combi = combi.filter(x => x.length > 1)
// Fetch data with combinations array    
    let drinkList = []
    let clen = combi.length

    const fetchAll = async (combi) => {
        for (let i = 0; i < clen; i++) {
            let possible = await axios.get(`https://www.thecocktaildb.com/api/json/v2/9973533/filter.php?i=${combi[i].join(',')}`)
            let data = possible.data.drinks
            typeof(data) !== 'string' && drinkList.push(data)
        }
        //flatten array of arrays into one.
        const concatList = drinkList.flat(1)
        let fullList = []
        let conLen = concatList.length
        //lookup each drink into new array with full details
        for (let i = 0; i < conLen; i++) {
            let res = await axios.get(`https://www.thecocktaildb.com/api/json/v2/9973533/lookup.php?i=${concatList[i].idDrink}`)
            let data = res.data.drinks
            fullList.push(data)
        }
        //Flatten array of arrays into single array
        fullList = fullList.flat(1)
        console.log(fullList)
        fullList.length > 0 && ingredients.push('Salt', 'Olive', 'Tea', 'Sugar')
        let result = []
        let valid = true;
*** // Code starts to fail below                             ***
        fullList.map(drink => {
            for (let i = 1; i <= 15; i++) {
                console.log(drink[`strIngredient${i}`])
                if (drink[`strIngredient${i}`] !== null && ingredients.indexOf(drink[`strIngredient${i}`]) === -1) {
                    valid = false;
                    break;
                } else if (ingredients.indexOf(drink[`strIngredient${i}`]) !== -1){
                    valid = true;
                }
                
            }
           valid === true && result.push(drink)
  ***     // End code fail                                     ***
        })
        console.log(result)
        // Filter result to remove duplicates
        const ids = result.map(x => x.idDrink)
        const filtered = result.filter(({idDrink}, index) => !ids.includes(idDrink, index + 1))
        setFiltResult(filtered)
    }
        fetchAll(combi) 
}
   useEffect(() => {
    getCombinations(ingredients);
   }, [clickState])
   
   
 console.log(filtResult)
   
  return (
    filtResult.length !== 0 ? filtResult.map(drink => {
        console.log(drink)
        const {strDrink, strDrinkThumb} = drink

        return (
            <>
                <h2>{strDrink}</h2>
                <img className="multi-search" src={strDrinkThumb} alt={strDrink} />
            </>
            
        )
    })
    :
    <h2>No search rn</h2> 
  )
}

export default MultiSearch

在此处输入图像描述

在此处输入图像描述

It seems like you are mutating the ingredients props in the part where you 'Flatten array of arrays into single array'.似乎您正在将“将数组扁平化为单个数组”的部分中的ingredients道具进行变异。 When ingredients is a local variable, it works fine but when you pass ingredients from props, it doesn't work in the expected way.ingredients是局部变量时,它可以正常工作,但是当您从道具传递ingredients时,它不会以预期的方式工作。 I think copying that prop to a local array might help.我认为将该道具复制到本地数组可能会有所帮助。

Props mutation道具突变

fullList.length > 0 && ingredients.push('Salt', 'Olive', 'Tea', 'Sugar')

Copying array in function scope在函数范围内复制数组

function getCombinations(valuesArray) {
  let originalIngredients = [...valuesArray];
}

Hope this works.希望这有效。

the fix:修复:

for (let i = 1; i <= 15; i++) {
               if (drink[`strIngredient${i}`] !== null && !newIngredients.includes(drink[`strIngredient${i}`].toLowerCase().replace(/\s/g, ''))) {
                   valid = false;
                   break;
               } else {
                   valid = true
               } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM