[英]Uncaught TypeError: myFormRef.submit is not a function, I tried to make my textarea in React submit the value with the key Enter
我試圖讓我在 React 中的 textarea 輸入使用 Enter 鍵提交值,但我收到了這個錯誤,我不知道為什么會這樣:
錯誤信息
這是我抓住關鍵的功能:
const myFormRef = useRef();
const onEnterPress = (e) => {
if(e.keyCode == 13 && e.shiftKey == false) {
e.preventDefault();
console.log(myFormRef.submit());
}
}
這是帶有文本區域的表單:
<Form id="myForm" ref={myFormRef} onSubmit={sendMessege}>
<TextareaAutosize
className='text_input'
placeholder='Digite algo aqui'
type="text"
value={formValue}
onChange={(e) => setFormValue(e.target.value)}
disabled={!user}
minRows={3}
maxRows={6}
onKeyDown={onEnterPress}
/>
</Form>
有人可以幫助我嗎?
您需要從參考中獲取current
。
請試試:
console.log(myFormRef?.current?.submit());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.