Точно так же вы знаете, что я новичок в javascript и реагируем на jj.Я работаю над проектом из Codecademy и застрял на коде ошибки.
Код ошибки, который я получаю:
TypeError: this.props.searchYelp is not a function. (In 'this.props.searchYelp(this.state.term, this.state.location, this.state.sortBy)', 'this.props.searchYelp' is undefined)
handleSearch
src/components/SearchBar/SearchBar.js:26
23 | }
24 |
25 | handleSearch(event) {
> 26 | this.props.searchYelp(this.state.term, this.state.location, this.state.sortBy);
^ 27 | event.preventDefault();
28 | }
29 |
enter code hereView compiled
Мой код SearchBar.js:
import React from 'react';
import './SearchBar.css';
const sortByOptions = {
'Best Match' : 'best_match',
'Highest Rated' : 'rating',
'Most Rated' : 'review_count'
};
class SearchBar extends React.Component {
constructor(props) {
super(props);
this.state = {
terms:'',
location:'',
sortBy:'best_match'
};
this.handleTermChange = this.handleTermChange.bind(this);
this.handleLocationChange = this.handleLocationChange.bind(this);
this.handleSearch = this.handleSearch.bind(this);
}
handleSearch(event) {
this.props.searchYelp(this.state.term, this.state.location, this.state.sortBy);
event.preventDefault();
}
handleTermChange(event) {
this.setState({ term: event.target.value });
}
handleLocationChange(event) {
this.setState({ location: event.target.value });
}
getSortByClass(sortByOption) {
if (sortByOption === this.state.sortBy) {
return 'active';
}
return '';
}
handleSortByChange(sortByOption) {
this.setState({ sortBy: sortByOption });
}
renderSortByOptions() {
return Object.keys(sortByOptions).map(sortByOption => {
let sortByOptionValue = sortByOptions[sortByOption];
return <li key={sortByOptionValue} onClick={this.handleSortByChange.bind(this, sortByOptionValue)} className={this.getSortByClass(sortByOptionValue)} >{sortByOption}</li>
});
}
render() {
return (
<div className="SearchBar">
<div className="SearchBar-sort-options">
<ul>
{this.renderSortByOptions()}
</ul>
</div>
<div className="SearchBar-fields">
<input placeholder="Search Businesses" onChange={this.handleTermChange} />
<input placeholder="Where?" onChange={this.handleLocationChange} />
</div>
<div className="SearchBar-submit">
<a onClick={this.handleSearch} href="www.#.com">Let's Go</a>
</div>
</div>
);
}
}
export default SearchBar;
Мой код App.js:
import React, { Component } from 'react';
import './App.css';
import SearchBar from './components/SearchBar/SearchBar';
import BusinessList from './components/BusinessList/BusinessList';
const business = {
imageSrc: 'https://s3.amazonaws.com/codecademy-content/programs/react/ravenous/pizza.jpg',
name: 'MarginOtto Pizzeria',
address: '1010 Paddington Way',
city: 'Flavortown',
state: 'NY',
zipCode: '10101',
category: 'Italian',
rating: 4.5,
reviewCount: 90
};
const businesses = [business, business, business, business, business, business];
class App extends Component {
seachYelp(term, location, sortBy) {
console.log(`Searching Yelp with ${term}, ${location}, and ${sortBy}`);
}
render() {
return (
<div className="App">
<h1>ravenous</h1>
<SearchBar searchYelp={this.searchYelp} />
<BusinessList businesses={businesses} />
</div>
);
}
}
export default App;
Кто-нибудь может мне помочь?Где я ошибся?
У меня в Google мой код ошибки, и я не могу найти решение ...
Также, пожалуйста, дайте мне знать, если вам нужна дополнительная информация ...
Спасибо!
Кэти