簡體   English   中英

我想將下面的 for 循環轉換為 array.map() 但不知道如何

[英]I want to convert below for loop into array.map() but not sure how to

OtpInput 是另一個組件,OTPLength 是從父組件獲得的道具,是必需的。

{
    const otp = getOtpValue()
    const inputs = []

    for (let index = 0; index < OTPLength; index++) {
      inputs.push(
        <Input
          key={index}
          focus={activeInput === index}
          value={otp[index]}
          onChange={handleOnChange}
          onKeyDown={handleOnKeyDown}
          onInput={handelOnInput}
          onInputFocus={onInputFocus}
          index={index}
          secure={secure}
          invalid={invalid}
          autoFocus={autoFocus}
        />
      )
    }

    return inputs
}

已經嘗試並更改了以下方式,但我只得到一個輸入,而不是 OTPLength 作為道具傳遞的值,然后用於基於 OTPLength 創建一個新數組。

const renderInputs = useMemo(() => {
    const otp = getOtpValue()
    const inputs = new Array(OTPLength)
    return [...inputs].map((_,index) =>
            <Input
                key={index}
                focus={activeInput === index}
                value={otp[index]}
                onChange={handleOnChange}
                onKeyDown={handleOnKeyDown}
                onInput={handelOnInput}
                onInputFocus={onInputFocus}
                index={index}
                secure={secure}
                invalid={invalid}
                autoFocus={autoFocus}
              />
            )
  }, [
    getOtpValue,
    OTPLength,
    activeInput,
    handleOnChange,
    handleOnKeyDown,
    handelOnInput,
    onInputFocus,
    autoFocus,
    invalid,
    secure
  ])

你甚至不需要map() ,你可以簡單地做:

const inputs = Array.from({length: OTPLength}, (_,i) => 
    <Input 
        key={i} 
        focus={activeInput == i} 
        /* the rest of your props */ 
    />
)

或者,如果您仍然喜歡map()

const inputs = [...Array(OTPLength)].map((_,i) => 
    <Input 
        key={i} 
        focus={activeInput == i} 
        /* the rest of your props */ 
    />
)

簡單地遍歷一個Array()map()第二個參數指的是當前索引

    const myArray = [...new Array(OTPLength)].map((obj, i)=> (
      <Input
        key={index}
        focus={activeInput === i}
        value={otp[i]}
        onChange={handleOnChange}
        onKeyDown={handleOnKeyDown}
        onInput={handelOnInput}
        onInputFocus={onInputFocus}
        index={index}
        secure={secure}
        invalid={invalid}
         autoFocus={autoFocus}
       />
));

下面的代碼創建一個與 OTPLength 大小相同的數組並返回一個 Input 元素數組

let otpArray = new Array(OTPLength);
return [...otpArray].map((element, index) => (
   <Input
       key={index}
       focus={activeInput === index}
       value={otp[index]}
       onChange={handleOnChange}
       onKeyDown={handleOnKeyDown}
       onInput={handelOnInput}
       onInputFocus={onInputFocus}
       index={index}
       secure={secure}
       invalid={invalid}
       autoFocus={autoFocus}
   />
))

這應該工作,

const renderInputs = useMemo(() => {
const otp = getOtpValue()
const inputs = new Array(OTPLength)
let ret = inputs.map((_,index) =>
        <Input
            key={index}
            focus={activeInput === index}
            value={otp[index]}
            onChange={handleOnChange}
            onKeyDown={handleOnKeyDown}
            onInput={handelOnInput}
            onInputFocus={onInputFocus}
            index={index}
            secure={secure}
            invalid={invalid}
            autoFocus={autoFocus}
          />
        );
        return ret;
 }, [
getOtpValue,
OTPLength,
activeInput,
handleOnChange,
handleOnKeyDown,
handelOnInput,
onInputFocus,
autoFocus,
invalid,
secure
])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM