[英]React: How to clear TextArea?
I know how to control input component.我知道如何控制输入组件。 But this behaviour I didn't understand at all.
但这种行为我完全不明白。
I have antd form with textarea in it.我有 antd 表单,里面有 textarea。
<Item
name='tags'
className='input-item tag-helper'
label='These are tags that appear on your NFT'
>
<TextArea value={text} className='big-text-area tag-text-area' placeholder='Enter a tag...' onKeyDown={addTag} />
</Item>
The textArea responsible for adding tags. textArea 负责添加标签。 So when I write something in input It controls by useState variable and after space press, It's added to an array with tags.
因此,当我在输入中写一些东西时,它由 useState 变量控制并在按空格键后,它被添加到带有标签的数组中。 After space press, input has to clear but nothing happens.
按空格键后,输入必须清除但没有任何反应。
My code looks like this right now:我的代码现在看起来像这样:
const addTag = value => {
setText(value.target.value)
if (value.keyCode === 32) {
setTags([...tags, text])
setText('')
}
}
In earlier version:在早期版本中:
const addTag = value => {
if (value.keyCode === 32) {
setTags([...tags, text])
setText('')
}
}
<Item
name='tags'
className='input-item tag-helper'
label='These are tags that appear on your NFT'
>
<TextArea value={text} className='big-text-area tag-text-area' placeholder='Enter a tag...' onKeyDown={addTag} onChange={v => setText(v.target.value)} />
</Item>
Also, I thought that It a good idea to try setting text through useEffect
, so I experimented and added the last one func:另外,我认为尝试通过
useEffect
设置文本是个好主意,所以我尝试并添加了最后一个 func:
useEffect(() => {
setText('')
}, [tags])
Can somebody say me what I'm doing wrong?有人可以说我做错了什么吗? Why I can't clear textArea after I press "space"?
为什么按“空格”后无法清除 textArea?
It could be something regarding the fact that you are not passing an onChange
prop to TextArea
so it is not in controlled mode and it is ignoring the value
prop you are passing to it.这可能与您没有将
onChange
道具传递给TextArea
的事实有关,因此它不在受控模式下,并且它忽略了您传递给它的value
道具。 You could try this:你可以试试这个:
<TextArea value={text} onChange={() => {}} className='big-text-area tag-text-area' placeholder='Enter a tag...' onKeyDown={addTag} />
or you can do this in the onChange
或者您可以在
onChange
中执行此操作
const onChange = (e) => {
const currentText = e.target.value;
if(currentText.length && currentText[currentText.length - 1] === " "){
setTags([...tags, text]);
setText("");
} else {
setText(e.target.value);
}
}
and leave the TextArea
like this并像这样离开
TextArea
<TextArea value={text} onChange={onChange} className='big-text-area tag-text-area' placeholder='Enter a tag...' />
Adding event.persist();添加 event.persist(); worked for me.
为我工作。
const addTag = event => {
event.persist();
if (event.keyCode === 32) {
setTags([...tags, text])
setText('');
}else{
setText(event.value);
}
};
I solved the task.我解决了任务。 I can't control the component because it's was wrapped in
Form.Item
, Item's has they own state, so It disturb to control Input
directly.我无法控制组件,因为它被包裹在
Form.Item
中,Item 有他们自己的 state,所以直接控制Input
会打扰。 Here is the changes:以下是更改:
<TextArea value={text} onChange={v => setText(v.target.value)} onKeyUp={addTag} className='big-text-area tag-text-area' placeholder='Enter a tag...' />
const addTag = value => {
if (value.keyCode === 32) {
setTags([...tags, text])
setText('')
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.