I want to store the value obtained after the api call in the state so that it can be rendered later on.I want the search term to be fetched from the api,i tried to use compontdidMount and conditionally fetch from the api but it didn't work as well.please help
import React, { Component } from 'react'
import axios from 'axios';
class New extends Component {
state = {
term:'',
res:[]
}
handleChange = e => {
this.setState({ term: e.target.value });
};
handleSubmit = e => {
e.preventDefault();
var url = `https://icanhazdadjoke.com/search?term=${this.state.term}`;
var options = {
method: 'GET',
headers: {
"Accept" : "application/json"
}
};
axios.get(url,options)
.then(response =>
this.setState({res:[response.data.results]})
)
.catch(error => {
// error.message is the error message
})
console.log(this.state)
};
render() {
return (<form className="jokes" onSubmit={this.handleSubmit}>
<input
onChange={this.handleChange}
value={this.state.title}
type="text"
name="term"
required
placeholder="cemetery"
/>
<button type="submit" value="Search" >Search</button>
</form> );
}
}
export default New;