** 尝试运行此代码时出现错误“TypeError:searchField.toLowerCase 不是函数”,而代码正在编译成功**
import React, { Component } from 'react';
import CardList from '../Components/CardList';
import SearchBox from '../Components/SearchBox';
//import { robots } from './robot';
import './App.css';
import Scroll from '../Components/Scroll';
import ErrorBoundry from '../Components/ErrorBoundry';
import { connect } from 'react-redux';
import { searchChange } from '../actions'
const mapStateToProps = state => {
return {searchField:state.searchField}
}
const mapDispatchToProps = (dispatch) =>{
return {
searchField:(event) => dispatch(searchChange(event.target.value))
}
}
class App extends Component {
constructor(){
super()
this.state = {
robots:[]
}
}
componentDidMount() {
fetch('https://jsonplaceholder.typicode.com/users').then(respond =>{
return respond.json()
}).then(users =>{
this.setState({robots:users})
})
}
render(){
const { searchField, onSearchChange } = this.props;
const filterRobots = this.state.robots.filter(robots =>{
//trying to search robot with either upper case or lower case
return robots.name.toLowerCase().includes(searchField.toLowerCase())
});
return(
<div className="tc">
<h1 className="head"> Robo Friend App</h1>
<div>
<SearchBox searchChange={onSearchChange}/>
</div>
<Scroll>
<ErrorBoundry>
<CardList robots={filterRobots} />
</ErrorBoundry>
</Scroll>
</div>
)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
慕斯王
凤凰求蛊
精慕HU
相关分类