第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 useRef 更改輸入值

使用 useRef 更改輸入值

慕桂英546537 2023-05-11 14:23:46
我用 React 的 useRef 鉤子捕獲了一個(gè)元素。如果我使用 console.log(this.inputRef) 我得到:<input aria-invalid="false" autocomplete="off" class="MuiInputBase-input-409 MuiInput-input-394" placeholder="Type ItemCode or scan barcode" type="text" value="2">有沒(méi)有辦法使用 this.inputRef 更改該元素的值?然后強(qiáng)制重新渲染?
查看完整描述

3 回答

?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

聽(tīng)起來(lái)您正在尋找的是ImperativeHandle掛鉤。

來(lái)自 React 文檔:

useImperativeHandle 自定義使用 ref 時(shí)暴露給父組件的實(shí)例值

下面的代碼應(yīng)該適合你:

function ValueInput(props, ref) {

? const inputRef = useRef();

? useImperativeHandle(ref, () => ({

? ? changeValue: (newValue) => {

? ? ? inputRef.current.value = newValue;

? ? }

? }));

? return <input ref={inputRef} aria-invalid="false" autocomplete="off" class="MuiInputBase-input-409 MuiInput-input-394" placeholder="Type ItemCode or scan barcode" type="text" value="2">

}

ValueInput = forwardRef(ValueInput);

查看完整回答
反對(duì) 回復(fù) 2023-05-11
?
猛跑小豬

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊

好吧,你可以這樣做:

<input ref={myRef} value={myRef.current.value} />

唯一的問(wèn)題是 refs DO NOT update or reredender the component, so, the value will never update... 而不是它可能會(huì)在您嘗試將不受控制的輸入作為受控輸入時(shí)拋出錯(cuò)誤


查看完整回答
反對(duì) 回復(fù) 2023-05-11
?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

也許這可以幫助


return(

    <input type="text" ref={inptRef}  />

    <button onClick={()=>inptRef.current.value = ""}>clear input</button>


)


查看完整回答
反對(duì) 回復(fù) 2023-05-11
  • 3 回答
  • 0 關(guān)注
  • 302 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)