3 回答
TA贡献1785条经验 获得超8个赞
简单地遍历一个空的 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}
/>
));
TA贡献1847条经验 获得超7个赞
你甚至不需要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 */
/>
)
TA贡献1812条经验 获得超5个赞
这应该有效,
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
])
添加回答
举报