亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React - 創建具有動態數量輸入的表單

React - 創建具有動態數量輸入的表單

侃侃爾雅 2023-09-21 16:53:17
我正在嘗試創建一個表單供用戶提交食譜。我需要將其設置為用戶可以單擊“+”按鈕來添加更多成分或添加更多說明的位置。每個輸入旁邊還有一個“-”按鈕可以將其刪除。我基本上可以正常工作,我可以添加項目。然而,刪除這些項目似乎沒有任何一致性。我正在使用 Material UI,我不確定這是否相關。以下是我正在處理的相關代碼,我將在最后鏈接完整的代碼和框。const [ingredients, setIngredients] = React.useState(  [    {      name: "",      amount: ""    }  ]);這是我在反應中渲染數組的方式        {ingredients.map((ing, idx) => {          return (            <div key={idx}>              <TextField                id={"ing-name-" + idx}                name={"ing-name-" + idx}                variant="outlined"                label="Ingrediant Name"                value={ing.name}                required                onChange={handleIngredientChange}              />              <TextField                id={"ing-amt-" + idx}                name={"ing-amt-" + idx}                variant="outlined"                label="Ingredient Amount"                value={ing.amount}                required                onChange={handleIngredientChange}              />              <Button                id={"ing-remove-" + idx}                variant="contained"                color="secondary"                type="button"                onClick={handleIngredientRemove}              >-</Button>            </div>          )        })}        <Button          variant="contained"          color="primary"          type="button"          onClick={handleIngredientAdd}        >+</Button>最后是處理添加/刪除項目的兩個函數  function handleIngredientRemove(event) {    /*    * To remove an element, we just use the array.filter function to genereate a new array without the     * element being deleted    */    console.log(event.target.id);    let idx = parseInt(event.target.id.split("-")[2]);    console.log("Removing ingredient " + idx);    let newIngredients = ingredients.filter((ingredient, index) => idx !== index);    setIngredients(newIngredients);      }完整的codesandbox演示https://codesandbox.io/s/happy-herschel-k0oqf
查看完整描述

3 回答

?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

Material UI 用多個標簽包裹你的按鈕。所以當你點擊一個按鈕時,里面可能還有其他東西被點擊。

將 id 傳遞給 onClick 函數將解決您的問題,但每次單擊按鈕時它可能會創建一個新函數。

onClick={()?=>?handleIngredientRemove(idx)}

我建議您使用currentTarget而不是 current。

function handleInstructionRemove(event) {

? ? // Use event.currentTarget instead of event.current

? ? let idx = parseInt(event.currentTarget.id.split("-")[2]);

? ? console.log("Removing instruction " + idx);

? ? let newinstructions = instructions.filter(

? ? ? ? ? (instruction, index) => idx !== index

? ? );

? ? setInstructions(newinstructions);

?}


查看完整回答
反對 回復 2023-09-21
?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

我建議不要解析元素 id 來提取索引。你可以直接傳遞:

onClick={() => handleIngredientRemove(idx)}

然后抓住它handleIngredientRemove就像

handleIngredientRemove(idx)

現在刪除得很好!


查看完整回答
反對 回復 2023-09-21
?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

一種簡單的方法是將索引傳遞給刪除處理程序并使用該索引進行過濾。(僅針對第一種形式進行了更改) 這里是更新后的代碼

或者您可以在添加時使用時間戳作為 id 并使用該唯一時間戳 id 進行刪除


查看完整回答
反對 回復 2023-09-21
  • 3 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號