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

110
Views
ReactJS component set state not doing anything?

I'm a newbie in ReactJS. I'm trying to do a searchbar as following:

class SearchBar extends Component {
  constructor(props) {
    super(props);

  this.state = {term: ''};
  }

  render() {
    return (
      <div>
        <input onChange={ event => this.setState({ term: event.target.value })} />
      </div>
    );
  }
}

It works but the problem is I couldn't get the changed value of the input. What's wrong with it? I couldn't find a solution. Thanks for your help.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You are not setting the value of the input. You need to change your input to controlled component which has its value set by state so it's value only changes when the state changes.

<input
      value = { this.state.term }
      onChange= { (event) => this.setState({ term: event.target.value }) } />

Thus, this.state causes the component to render and when it re-renders the value of the element(input-in example) is set to the new value of this.state.term.

over 4 years ago · Santiago Trujillo Report

0

You're not assigning a value to the input.

<input value={this.state.term} onChange={ event => this.setState({ term: event.target.value })} />
over 4 years ago · Santiago Trujillo 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!