我想提前道歉,因为我是使用React Framework的新手,并且我的代码编写不干净。我目前正在使用选择器为API中的JSON数据在代码中创建--Filter By--下拉列表。但是我的代码中不断出现这种错误。在过去的两天里,我一直在处理此问题,但至今仍无法解决。
我的代码:
selector.js
import { createSelector } from "reselect";
const getItems = (state) => [state.shop];
const getValue = (state) => state.category;
export const getFilteredItems = createSelector(
[getItems, getValue],
(items, value) => {
items =
value == "ALL"
? items
: items.filter((item) => item.productIdentifier.includes(value));
}
);
catalog.jsx->组件
import React, { Component } from "react";
import "./CatalogStyle.css";
import { addToCart, getItems } from "../../actions/projectActions";
import { getFilteredItems } from "../../selectors/selector";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
class Catalog extends Component {
constructor(props) {
super(props);
this.state = {
category: "ALL",
};
}
componentDidMount() {
this.props.getItems();
}
filterByCategory = (e) => {
let filterValue = e.target.value;
this.setState({ category: filterValue });
};
_addToCart = (id) => {
this.props.addToCart(id);
};
render() {
const { items } = this.props.items;
return (
<React.Fragment>
<div>
<label>
Filter By:
<select
value={this.state.value}
onChange={(e) => {
this.filterByCategory(e);
}}
>
<option value="ALL">All</option>
<option value="FD">Food</option>
<option value="CL">Clothes</option>
</select>
</label>
</div>
<div className="table">
{items.map((item) => (
<div className="card" key={item.id}>
<img
className="imageProperty"
src={require("../layout/images/buat_gambaran_product/u138.jpg")}
></img>
<h3 className="productName">{item.productName}</h3>
<p className="PriceAmmount">{item.productPrice}</p>
<p>{item.productDescription}</p>
<Link to="/checkout">
<button
onClick={() => {
this._addToCart(item.id);
}}
>
add
</button>
</Link>
</div>
))}
</div>
</React.Fragment>
);
}
}
Catalog.propTypes = {
getItems: PropTypes.func.isRequired,
items: PropTypes.object.isRequired,
};
const mapStateToProps = (state) => ({
items: getFilteredItems(state),
});
const mapDispatchToProps = (dispatch) => {
return {
addToCart: (id) => {
dispatch(addToCart(id));
},
getItems: () => {
dispatch(getItems());
},
};
};
export default connect(mapStateToProps, mapDispatchToProps)(Catalog);
我的reducer.js以获取所有项目
case GET_ITEMS:
return {
...state,
items: action.payload,
};
我对getItems的projectActions.js
export const getItems = () => async (dispatch) => {
const res = await axios.get("http://localhost:8080/api/project/all");
dispatch({
type: GET_ITEMS,
payload: res.data,
});
};
JSON
shop : {
items : [
0{
"id": 1,
"productName": "shoes",
"productIdentifier": "CL001",
"productDescription": "adidas kicks boir",
"productPrice": 2000,
"productStock": 200,
"created_at": "2020-51-28",
"updated_at": null
}
1{
"id": 2,
"productName": "burger",
"productIdentifier": "FD001",
"productDescription": "charsiu berger",
"productPrice": 2000,
"productStock": 200,
"created_at": "2020-51-28",
"updated_at": null
}
],
}
PS。请不要介意created_at
和updated_at
。
错误日志
Uncaught TypeError: Cannot read property 'includes' of undefined
at selector.js:9
at Array.filter (<anonymous>)
at selector.js:9
at index.js:69
at index.js:30
at index.js:82
at index.js:30
at Function.mapStateToProps [as mapToProps] (Catalog.jsx:83)
at mapToPropsProxy (wrapMapToProps.js:41)
at Function.detectFactoryAndVerify (wrapMapToProps.js:50)
at mapToPropsProxy (wrapMapToProps.js:41)
at handleFirstCall (selectorFactory.js:22)
at pureFinalPropsSelector (selectorFactory.js:63)
at connectAdvanced.js:208
at mountMemo (react-dom.development.js:16875)
at Object.useMemo (react-dom.development.js:17187)
at useMemo (react.development.js:1614)
at ConnectFunction (connectAdvanced.js:193)
at renderWithHooks (react-dom.development.js:16317)
at updateFunctionComponent (react-dom.development.js:18363)
at updateSimpleMemoComponent (react-dom.development.js:18303)
at updateMemoComponent (react-dom.development.js:18208)
at beginWork$1 (react-dom.development.js:20234)
at HTMLUnknownElement.callCallback (react-dom.development.js:337)
at Object.invokeGuardedCallbackDev (react-dom.development.js:386)
at invokeGuardedCallback (react-dom.development.js:439)
at beginWork$$1 (react-dom.development.js:25768)
at performUnitOfWork (react-dom.development.js:24685)
at workLoopSync (react-dom.development.js:24658)
at performSyncWorkOnRoot (react-dom.development.js:24247)
at react-dom.development.js:12285
at unstable_runWithPriority (scheduler.development.js:701)
at runWithPriority$2 (react-dom.development.js:12231)
at flushSyncCallbackQueueImpl (react-dom.development.js:12280)
at flushSyncCallbackQueue (react-dom.development.js:12268)
at discreteUpdates$1 (react-dom.development.js:24401)
at discreteUpdates (react-dom.development.js:1439)
at dispatchDiscreteEvent (react-dom.development.js:5914)
在此先感谢任何可以帮助我的人。
答案 0 :(得分:0)
问题在于,在items
数组中,您没有具有属性productIdentifier
的对象,或者,也许,有些对象没有此属性。它调用错误:
selector.js
import { createSelector } from "reselect";
const getItems = (state) => [state.shop];
const getValue = (state) => state.category;
export const getFilteredItems = createSelector(
[getItems, getValue],
(items, value) => {
items =
value == "ALL"
? items
: items.filter((item) => item.productIdentifier.includes(value)); //<== this
}
);
答案 1 :(得分:0)
欢迎使用Stack Overflow和React技术。
您的getItems
对象为数组类型,因此您需要遍历变量。
因此,由于直接应用item.productIdentifier.includes(value))
会导致错误Undefined
,就像item
一样,第二件事是数组的索引,然后是{{1 }}对象键已定义。
在下面查看代码段。
productIdentifier