我想在keydownListener这个方法中去取上面定义好的最新的value的值,但是发现每次取到的都是初始的值。哪位大神一页到过这中问题,烦请解答一下,不胜感激。functionInput(props){letinputRef=useRef(null);let{value,handleChange,handleKeyDown,handleBlur,handleFocus}=useInputValue(inputRef,props.addTodo);return(ref={inputRef}className={styles['input']}value={value}onFocus={handleFocus}onBlur={handleBlur}//onKeyDown={handleKeyDown}onChange={handleChange}placeholder={'Whatneedstobedone?'}/>)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);//????这个地方取到的值始终都是''setValue('');}}return{value,handleChange,handleBlur,handleFocus}
潇湘沐
相关分类