I try some similar solutions I find on stack overflow but nothing seems to work, this is minor problem and according to code it should work, only problem that is puzzling me is maybe that after I submit the form app control is handed over to parent class, one level up which has nothing to do with state, and my setState method is called after this.props.api method in child component, I hope someone could help me with this one...
HERE IS THE CODE FROM CHILD
class UserInput extends Component{
constructor(props){
super(props)
this.state = {
value: ''
}
}
handleChange = (event) => {
this.setState({value: event.target.value});
}
onFormSubmit = (e) => {
e.preventDefault();
this.props.api(this.state.value);
this.setState({
value:''
});
}
render(){
return (
<div>
<form noValidate autoComplete="off" onSubmit={this.onFormSubmit}>
<TextField onChange={this.handleChange}
id="outlined-name"
label="Image Search"
margin="normal"
variant="outlined"
style={{minWidth:'500px'}}
type="text"
/><br></br>
<Button
variant="contained"
color="primary"
type="submit"
>
AND THIS IS CODE FROM PARENT CLASS
class App extends React.Component {
async onFormSubmit(term){
const res = await axios.get('https://api.unsplash.com/search/photos', {
params:{query: term},
headers:{
Authorization: 'Client-ID'
}
});
console.log(res.data.results)
}
render(){
return (
<div className="App">
<UserInput api={this.onFormSubmit}/>
<List/>