简体   繁体   English

如何将输入值匹配到 a.map function 从 api 提取数据

[英]how to match an inputs value to a .map function pulling data from an api

I have a.map function displaying data that is being pulled from an api, I would like to add a field to the data and have it match to the relevant piece of data.我有一个.map function 显示从 api 中提取的数据,我想在数据中添加一个字段并使其与相关数据相匹配。

Fo example I am pulling ingredients from an api that contains quantities, I would like to add an input field that would allow me to add a multiplier that I could later use to multiply the ingredients.例如,我正在从包含数量的 api 中提取成分,我想添加一个输入字段,允许我添加一个乘数,以后可以使用它来乘以成分。

Here is the.map function:这是.map function:

 <div className="mt-4 border-t border-b border-gray-200 divide-y divide-gray-200">
              {data.ingredients.map((ingredient) => (
                <div
                  key={ingredient.id}
                  className="relative flex items-start py-4"
                >
                  <div className="ml-4 flex items-center h-5">
                    <input
                      onChange={selectIngredient(ingredient.id)}
                      value={selectedIngredients.indexOf(ingredient.id === -1)}
                      type="checkbox"
                      className="focus:ring-purple-500 h-4 w-4 text-purple-600 border-gray-300 rounded"
                    />
                  </div>

                  <div className="ml-3 text-sm">
                    <label
                      className="font-medium text-gray-700 select-none"
                    >
                      {ingredient.name}
                    </label>
                  </div>
                  <div className="ml-3 text-sm">
                  // Here I am using useState to set a multiplier, but it sets the same vale for all data in the .map list.
                    <input
                      value={multiplier}
                      onChange={(e) => setMultiplier(e.target.value)}
                      type="number"
                      className="flex-1 focus:ring-purple-500 focus:border-purple-500 block w-full min-w-0 rounded-md sm:text-sm border-gray-300"
                    />
                  </div>
                </div>
              ))}
            </div>

Here's what an ingredient object looks like:下面是成分 object 的样子:

ingredient: {
id: 1,
name: sugar,
quantity: 100,
quantityUnit: grams,
}

Is there a way I set an piece of state that is linked to the relevant mapped piece of data?有没有办法设置一个链接到相关映射数据的 state ?

In your component, create a new state.在您的组件中,创建一个新的 state。 Let's call this the ingredients state that has a setIngredients dispatcher.我们将其称为具有setIngredients调度程序的ingredients state。 Set it by default as an empty array.默认情况下将其设置为空数组。

const [ingredients, setIngredients] = useState([]);

With a useEffect hook, loop over the received data with the .map() method and return a new object from each ingredient with a multiply property added.使用useEffect钩子,使用.map()方法循环接收到的数据,并从添加了multiply属性的每种成分返回一个新的 object。 Update the ingredients state with this new array of objects.使用这个新的对象数组更新ingredients state。

useEffect(() => {
  if (data.length) {
    setIngredients(
      data.map(ingredient => ({
        ...ingredient,
        multiply: 1
      })
    );
  }
}, [data]);

Now your ingredients state has all ingredients plus a multiply property for each ingredient.现在您的ingredients state 具有所有成分以及每种成分的multiply属性。

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

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