Skip to content

Instantly share code, notes, and snippets.

@pinglinh
Created March 26, 2018 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinglinh/4428b65b524f203535242ca69e26f47e to your computer and use it in GitHub Desktop.
Save pinglinh/4428b65b524f203535242ca69e26f47e to your computer and use it in GitHub Desktop.
Refactoring React components : SearchContainer 1.3.3
import React from "react";
import Search from "../../components/Search";
class SearchContainer extends React.Component {
constructor(props) {
super(props);
this.state = {
value: "",
articles: []
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ value: event.target.value });
fetch(`http://content.guardianapis.com/search?q=${event.target.value}&api-key=YOUR_API_KEY`)
.then(response => response.json())
.then(data => this.setState({ articles: data.response.results }));
}
handleSubmit(event) {
event.preventDefault();
}
render() {
return (
<Search
handleSubmit={this.handleSubmit}
value={this.state.value}
performSearch={this.handleChange}
articles={this.state.articles}
/>
);
}
}
export default SearchContainer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment