Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

388
Views
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.

<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. So when I write something in input It controls by useState variable and after space press, It's added to an array with tags. 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(() => {
    setText('')
}, [tags])

Can somebody say me what I'm doing wrong? Why I can't clear textArea after I press "space"?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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. 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

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 value={text} onChange={onChange} className='big-text-area tag-text-area' placeholder='Enter a tag...' />
about 4 years ago · Juan Pablo Isaza Report

0

Adding event.persist(); worked for me for clearing the textarea, with regards to tags list it depends entirely depends on what your initial state is and how you are managing with tags array.

  const [text, setText] = useState('');
  const [tags, setTags] = useState([]);

  const addTag = event => {
      event.persist();
      console.log(tags);
      setText(event.value);
      if (event.keyCode === 32) { 
          setText('');   
          setTags((previousTags) => {
             return [...previousTags,event.target.value];
          });        
      }
  };

 // textarea are I used 
<textarea value={text} className='big-text-area tag-text-area' placeholder='Enter a tag...' onChange={addTag} onKeyDown={addTag} />
about 4 years ago · Juan Pablo Isaza Report

0

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. 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('')
  }
} 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!