javascript - react js中的过滤器错误无法读取reactjs中未定义的属性'indexOf'
问题描述
我正在使用过滤器过滤一个对象数组,我通过 axios 从 api 获取它并将其存储在 redux 状态它适用于映射但使用过滤器它会给出此错误 Cannot read property 'indexOf' of undefined in reactjs
class ProductsList extends Component {
constructor(props) {
super(props)
this.state = {
search : ''
}
}
searchUpdate(event) {
this.setState({search: event.target.value.substr(0,20)})
}
async componentDidMount() {
const { fetchProducts } = this.props;
await fetchProducts();
}
async componentDidUpdate() {
const { fetchProducts } = this.props;
await fetchProducts();
}
render() {
//The problem with this filter method however if I don't use it it works will
let productsFiltered = this.props.products.filter((itemProduct)=>{
return itemProduct.nameEn.indexOf(this.state.search) !== -1
} )
const mapStateToProps = state => {
return {
products: state.categoryReducer.products
}
}
…………
export default connect(mapStateToProps, actions)(ProductsList);
解决方案
推荐阅读
- html - 尝试制作注册表单,但出现“SignupComponent”类型上不存在属性“地址”的错误?还有更多人喜欢它
- swiftmailer - 要在 WAMP 中使用 Swiftmailer,WAMP 中的 /usr/local/lib/php 等价物是什么?
- ios - Cocoapods 错误:无法为“RNCryptor”加载底层模块
- pygame - Pygame 窗口突然没有响应
- c - 错误:一元'*'的无效类型参数(有'int')|
- flutter - 颤动本地通知“一天中的小时,以 24 小时制 [0..23] 表示。”
- wpf - 使用异步和等待实时更新数据
- cassandra - 如何使用图形引擎作为 Janusgraph 和存储后端作为 cassandra 更改 gremlin 中的属性键?
- linux - 安装后无法运行mongoDB
- javascript - 如何在 React 中有条件地设置状态?