Answer the question
In order to leave comments, you need to log in
Why is the event not firing correctly?
I have this React component:
import React from 'react';
import './AppSearch.css'
class AppSearch extends React.Component {
constructor() {
super();
this.state = {
request: '',
}
this.onSearch = (e) => {
this.setState({
request: e.target.value,
})
console.log(this.state.request);
}
}
render () {
return(
<input className='AppSearch' value={this.state.request} onChange={ this.onSearch } placeholder = 'Search parametr' />
)
}
};
export default AppSearch;
Answer the question
In order to leave comments, you need to log in
First, events do not work - none and never. Event handlers fire. Secondly - between "wrong" and "not the way I would like" there is a significant difference, try to realize it.
The setState method is asynchronous. If you want to access the changed state, use the callback:
this.setState({
request: e.target.value,
}, () => console.log(this.state.request))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question