reactjs - this.props.xxx 不是函数
问题描述
我发现了一些其他有类似错误的帖子,我已经排除了:
该函数在花括号中正确导入
动作已发送
文件路径正确
这一定是一些愚蠢的错误,但我已经看了很长时间了,似乎找不到它,感谢您的帮助!
行动:
export const deleteRow = (rowId) => (dispatch) => {
dispatch({ type: "LOADING_UI" });
axios
.delete(`/row/${rowId}`)
.then((res) => {
dispatch({
type: "DELETE_ROW",
payload: res.data,
});
dispatch(clearErrors());
})
.catch((err) => {
dispatch({
type: "SET_ERRORS",
payload: err.response.data,
});
});
};
零件:
export class MyDelete extends Component {
state = {
open: false,
};
handleOpen = () => {
this.setState({ open: true });
};
handleClose = () => {
this.setState({ open: false });
};
handleDelete = () => {
this.props.deleteRow(this.props.rowId);
this.setState({ open: false });
};
render() {
return (
<Fragment>
<Button onClick={this.handleOpen}>
<HighlightOffIcon color="primary" />
</Button>
<Dialog
open={this.state.open}
onClose={this.handleClose}
fullWidth
maxWidth="sm"
>
<DialogTitle>Are you sure you want to delete this row ?</DialogTitle>
<DialogActions>
<Button onClick={this.handleClose} color="primary">
Cancel
</Button>
<Button onClick={this.handleDelete} color="secondary">
Delete
</Button>
</DialogActions>
</Dialog>
</Fragment>
);
}
}
MyDelete.propTypes = {
deleteRow: PropTypes.func.isRequired,
rowId: PropTypes.string.isRequired,
};
export default connect(null, { deleteRow })(MyDelete);
解决方案
尝试使用mapDispatchToProps
函数方法而不是像这样的对象速记形式:-
const mapDispatchToProps = (dispatch) =>{
return {
deleteRow:(rowId)=>dispatch(deleteRow(rowId));
}
}
export default connect(null, mapDispatchToProps)(MyDelete);
推荐阅读
- python - Python:如何创建单元组列表?
- amazon-web-services - 具有加密启动卷的 CloudFormation Launch 配置
- javascript - JS:文本文件到 JSON
- c# - EmguCV Out of memory exception in x86 release mode only-Sharpening Images
- c# - VS Debugger 将 foreach 循环中的当前值显示为 null,而它不是
- javascript - setValue 不是 EelementRef 的属性
- react-native - 哪个 react-native 版本支持所有 npms?
- visual-studio-cordova - VS Tools for Cordova 覆盖 Android 图标
- mongodb - Mongo DB 或任何 nosql DB(Hbase、Cassandra)如何比传统的 RDBMS 具有可扩展性和优势?
- php - 使用 Laravel 的 Eloquent 构建查询