您将要为此创建一个选择器,您可以从中使用
mapStateToProps
function getTotalCost (state) {
return state.reduce((result, item) => item.qty * item.price + result, 0);
}
在组件中..
import { getTotalCost } from "./items/selectors";
const mapStateToProps = state => {
return {
totalCost: getTotalCost(state.items)
}
}
class Component extends React.Component {
render() {
const { totalCost } = this props;
...
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Component)