I have a search bar and a card that displays a dog name. I want to use the search bar to filter the dog's name. Right now I am getting the error props.dogs.map is not a function.
In the DogCard component, I have changed props.dogs to props.filteredName but I am still getting the error. I also looked at ReactJS with ES6: this.props is not a function when I communicate two components, because it was similar to my error. I have tried handleFilterName={this.handleFilterName.bind(this)} and handleFilterName={e => this.handleFilterName(e)} but I am still getting the same error.
class SelectDogs extends Component {
state = {
dogs:
[ {
name: 'Jack',
breed: 'beagle',
age:'2',
}
],
filteredName: ''
}
handleFilterName = (e) => {
const dogs = this.state.dogs
const filteredDogName = dogs.filter( dog => {
return (
dog.name.search(e.currentTarget.value) !== -1
);
})
this.setState({filteredName: filteredDogName})
}
render() {
return (
<div>
<NavHeader/>
<SearchDog handleFilterName={this.handleFilterName}/>
<DogCard dogs={this.state.filteredName}></DogCard>
</div>
)
}
}
export default SelectDogs
const SearchDog = (props) => {
return (
<div className="search-container">
<Form inline onChange={props.handleFilterName}>
<FormControl type="text" placeholder="Search a Dog" className=" mr-sm-2" />
<Button type="submit"><FaSistrix/></Button>
</Form>
</div>
)
}
export default SearchDog
const DogCard = (props) => {
return props.dogs.map( dog =>
{
return <div className="dog-container"><Card className="dog-card" key={dog.name}><Link to='/dog'>{dog.name}</Link></Card></div>
})
}
export default DogCard