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

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

React hooks組件中,定義的方法為什么取不到新的state的值

React hooks組件中,定義的方法為什么取不到新的state的值

我想在keydownListener這個(gè)方法中去取上面定義好的最新的value的值,但是發(fā)現(xiàn)每次取到的都是初始的值。哪位大神一頁到過這中問題,煩請(qǐng)解答一下,不勝感激。functionInput(props){letinputRef=useRef(null);let{value,handleChange,handleKeyDown,handleBlur,handleFocus}=useInputValue(inputRef,props.addTodo);return()exportconstuseInputValue=(inputRef,addTodo)=>{let[value,setValue]=useState('');letfocusStatus=useRef(false);consthandleChange=useCallback((e)=>{letv=e.target.value;setValue(v);},[])consthandleFocus=()=>{focusStatus.current=true;inputRef.current.addEventListener('keydown',keydownListener);}consthandleBlur=()=>{focusStatus.current=false;inputRef.current.removeEventListener('keydown',keydownListener);}constkeydownListener=(e)=>{if(e.keyCode===13&&focusStatus.current){addTodo(value);//????這個(gè)地方取到的值始終都是''setValue('');}}return{value,handleChange,handleBlur,handleFocus}
查看完整描述

2 回答

?
慕容708150

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

functionuseClosure(fn){
constfnRef=useRef();
constfnProxy=useCallback(()=>fnRef.current(),[]);
useEffect(()=>{
fnRef.current=fn;
});
returnfnProxy;
}
constuseInputValue=(inputRef,addTodo)=>{
let[value,setValue]=useState('');
//...
constkeydownListener=useClosure((e)=>{
if(e.keyCode===13&&focusStatus.current){
addTodo(value);//總會(huì)取到當(dāng)前的value
setValue('');
}
});
//...
};
                            
查看完整回答
反對(duì) 回復(fù) 2019-05-08
  • 2 回答
  • 0 關(guān)注
  • 2996 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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