My issue here is that I have some values coming up from the SearchForm component. They're passing the correct value as arguments to the handleSearch function, but my call to setState does nothing. I've included console.logs to show what's stored in the variables. See comments in the code below.
Because my state is being passed from this component to the ResultList component as empty strings, the ResultList does not render correctly.
import React, { Component } from 'react';
import axios from 'axios';
import SearchForm from './components/search_form';
import ResultList from './components/results_list';
class App extends Component {
constructor(props) {
super(props);
this.state = { category: '', term: '', results: [] };
this.handleSearch = this.handleSearch.bind(this);
}
handleSearch(category, term) {
//This is not setting the state!!!!!
this.setState({ category, term });
//The first two console.logs successfully log the arguments to this
function
//The last two console.logs log empty strings even after the above
setState call.
console.log("Received from form: " + category);
console.log("Received from form: " + term);
console.log("#################################");
console.log(this.state.category);
console.log(this.state.term);
console.log('http://swapi.co/api/' + category + '/?search=' + term);
axios.get('http://swapi.co/api/' + category + '/?search=' +
term).then((response) => {
let results = response.data.results;
this.setState({ results });
console.log(this.state.results);
}).catch((error) => {
console.log(error);
});
}
render() {
return (
<div className="container panel panel-default">
<SearchForm handleSearch={this.handleSearch}/>
<ResultList results={this.state.results} category=
{this.state.category}/>
</div>
);
}
}
export default App;