reactjs - 如何在 const export “function” 周围包装 HOC
问题描述
我有一个调用的 HOC withErrorHandler
,它接受一个组件,axios
它使用它并向它添加错误处理程序,这对于普通组件很容易
export default withErrorHandler(myComponent, axios);
但是当导出语法和函数是这样的时候,我不知道如何做同样的事情:
//how can i wrap an hoc around this?
export const authenticationService = {
login,
logout,
currentUser: currentUserSubject.asObservable(),
get currentUserValue() { return currentUserSubject.value }
};
function login(username, password) {}
function logout() {}
更新 01:
我的 HOC 组件:
import React, { Component } from 'react';
import Dialog from '@material-ui/core/Dialog';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogTitle from '@material-ui/core/DialogTitle';
import Paper from '@material-ui/core/Paper';
// import Draggable from 'react-draggable';
// function PaperComponent(props) {
// return (
// <Draggable cancel={'[class*="MuiDialogContent-root"]'}>
// <Paper {...props} />
// </Draggable>
// );
// }
const withErrorHandler = (WrappedComponent, axios) => {
return class extends Component {
state = {
error: null
}
componentWillMount() {
axios.interceptors.request.use(req => {
this.setState({ error: null });
return req;
});
axios.interceptors.response.use(res => res, error => {
this.setState({ error: error });
});
}
errorConfirmedHandler = (event) => {
this.setState({ error: null });
}
render() {
console.log('inside with error handler')
return (
<React.Fragment>
<Dialog
open={this.state.error}
onClose={this.errorConfirmedHandler}
//PaperComponent={PaperComponent}
aria-labelledby="draggable-dialog-title"
>
<DialogTitle style={{ cursor: 'move' }} id="draggable-dialog-title">
خطا
</DialogTitle>
<DialogContent>
<DialogContentText>
{this.state.error ? this.state.error.message : null}
</DialogContentText>
</DialogContent>
</Dialog>
<WrappedComponent {...this.props} />
</React.Fragment>
);
}
}
}
export default withErrorHandler;
解决方案
推荐阅读
- flutter - 颤振:'未来
' 不是 bool 类型的子类型 - excel - 内存不足 VBA 错误,我什至无法打开开发人员窗口(alt + F11)
- gradle - 使用 Kotlin 和 Gradle 创建一个胖 jar - 编译与实现?
- c# - 尝试在广告关闭但不保持隐藏时隐藏“删除广告”按钮
- intellij-idea - Intellij shortuct 在两条线之间创建一条线?
- apollo - ApolloClient:订阅删除后 UI (ROOT_QUERY) 未更新
- node.js - 无法使用 Ajax 和 Multer 上传文件图像
- c# - 为什么无法在 csom 中初始化项目的 FieldLookupValue?
- c - C中将uint16_t和int16_t合并成int32_t进行数据传输
- c# - 通用主机生成器