JavaScript Clear textarea after enter key clear textarea after submit,clear textarea after submit react,clear

I would like to clear the textarea content after enter key press on react. However when enter key press the text area clears but the enter key remains (new line). How can I fix this?

constructor(){
    this.state = { text : '' }
}

keypress(e){ 
   if(e.key == 'Enter'){
      this.setState({
        send_message: ''
      })
   }
}

<textarea value={this.state.text} placeholder="Text" rows="1" className="form-textarea typing-area" onKeyPress={this.keypress.bind(this)}></textarea>
Answer:1

You can use event.preventDefault when the key is 'Enter':

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { text : '' }
    this.handleChange = this.handleChange.bind(this);
    this.keypress = this.keypress.bind(this);
  }
  
  handleChange(event) {
    this.setState({text: event.target.value});
  }
  
  keypress(e){ 
    if(e.key === 'Enter'){
      e.preventDefault();
      this.setState({
        send_message: this.state.text,
        text: '',
      });
    }
  }
  render() {
    return (
      <div>
        <textarea 
          value={this.state.text} 
          placeholder="Text" 
          rows="1" 
          className="form-textarea typing-area" 
          onChange={this.handleChange}
          onKeyPress={this.keypress} 
        />
        <pre>{JSON.stringify(this.state, null, 2)}</pre>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>
Answer:2

There is a onclick function tied up with this tag. I need to trigger this onclick function, so I used page.sendEvent('click', element.offsetLeft, element.offsetTop, 'left'); But it ...

There is a onclick function tied up with this tag. I need to trigger this onclick function, so I used page.sendEvent('click', element.offsetLeft, element.offsetTop, 'left'); But it ...

This is a LoremPixel component that I built: export default function LoremPixel({ url = 'http://lorempixel.com', width = 200, height = 400, alt = 'Placeholder image' }) { const src = `${url}/${...

This is a LoremPixel component that I built: export default function LoremPixel({ url = 'http://lorempixel.com', width = 200, height = 400, alt = 'Placeholder image' }) { const src = `${url}/${...

I'm trying to parse the following data: data = [ {"id":"orderBy", "options" : [ {"value":"order-by=newest&", "name":"newest"}, {"value":"order-by=relevance&", "name":"...

I'm trying to parse the following data: data = [ {"id":"orderBy", "options" : [ {"value":"order-by=newest&", "name":"newest"}, {"value":"order-by=relevance&", "name":"...

  1. accessing structure data in matlab
  2. accessing structure data

So I'm trying to create a program that iterates through 2D array until it finds a matching ticket; however when the program finds the number within the array it doesn't break: var namesArray = [...

So I'm trying to create a program that iterates through 2D array until it finds a matching ticket; however when the program finds the number within the array it doesn't break: var namesArray = [...

  1. legal issues with breaking a lease