Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChris Carr
Front End Web Development Techdegree Student 12,900 PointsError in console
Uncaught TypeError: _this.props.onSearch is not a function
and no gifs display in the browser.
import React, { Component } from 'react';
export default class SearchForm extends Component {
state = {
searchText: ''
}
onSearchChange = e => {
this.setState({ searchText: e.target.value });
}
handleSubmit = e => {
e.preventDefault();
this.props.onSearch(this.state.searchText);
e.currentTarget.reset();
}
render() {
return (
<form className="search-form" onSubmit={this.handleSubmit} >
<label className="is-hidden" htmlFor="search">Search</label>
<input type="search"
onChange={this.onSearchChange}
name="search"
placeholder="Search..." />
<button type="submit" id="submit" className="search-button"><i className="material-icons icn-search">search</i></button>
</form>
);
}
}
Then main component ...
import React, { Component } from 'react';
import './App.css';
import axios from 'axios';
import SearchForm from './Components/SearchForm';
import GifList from './Components/GifList';
const API_Key = 'G7l64tPC4irRIG4kFZlZo7oT57QBo5nK';
export default class App extends Component {
constructor() {
super();
this.state = {
gifs: []
};
}
performSearch = (query) => {
axios.get(`https://api.giphy.com/v1/gifs/search?q=${query}&limit=24&api_key=G7l64tPC4irRIG4kFZlZo7oT57QBo5nK`)
.then( response => {
this.setState({
gifs: response.data.data
});
})
.catch( response => {
console.log('Error parsing and fetching data');
});
}
render() {
return (
<div>
<div className="main-header">
<div className="inner">
<h1 className="main-title">GifSearch</h1>
<SearchForm onSearch={this.peformSearch} />
</div>
</div>
<div className="main-content">
<GifList data={this.state.gifs} />
</div>
</div>
);
}
}
2 Answers
Tianni Myers
10,453 PointsWhere is the code?
gqtojlonge
1,716 PointsHi Chris, Try binding this to the form function. <SearchForm onSearch={this.performSearch.bind(this)} />
Chris Carr
Front End Web Development Techdegree Student 12,900 PointsChris Carr
Front End Web Development Techdegree Student 12,900 PointsPost edited. Thanks!