我正在尝试通过向用户发送警报来使错误处理正常进行,我已经将问题选出到一个组件中,并且尝试将其呈现在哪里,我已经在文档中搜索了有关其原因的所有答案。没有工作,什么也没发现。也许我看不到我面前的东西?
这是给我带来麻烦的警报组件。
class Alerts extends Component {
static propTypes = {
error: PropTypes.object.isRequired,
message: PropTypes.object.isRequired
};
componentDidUpdate(prevProps) {
const { error, alert, message } = this.props;
if (error !== prevProps.error) {
if (error.msg.name) alert.error(`Name: ${error.msg.name.join()}`);
if (error.msg.email) alert.error(`Email: ${error.msg.email.join()}`);
if (error.msg.message)
alert.error(`Message: ${error.msg.message.join()}`);
if (error.msg.non_field_errors)
alert.error(error.msg.non_field_errors.join());
if (error.msg.username) alert.error(error.msg.username.join());
}
if (message !== prevProps.message) {
if (message.deleteItem) alert.success(message.deleteItem);
if (message.addItem) alert.success(message.addItem);
if (message.passwordNotMatch) alert.error(message.passwordNotMatch);
}
}
render() {
return <Fragment />;
}
}
const mapStateToProps = state => ({
error: state.errors,
message: state.messages
});
export default connect(mapStateToProps)(withAlert(Alerts));
这就是我所说的:
const alertOptions = {
timeout: 3000,
position: "top center"
};
class App extends Component {
componentDidMount() {
store.dispatch(loadUser());
}
render() {
return (
<ReduxProvider store={store}>
<Provider template={AlertTemplate} {...alertOptions}>
<Router>
<Fragment>
<Header />
<Alerts />
<div className="container">
<Switch>
<PrivateRoute exact path="/" component={Dashboard} />
<Route exact path="/register" component={Register} />
<Route exact path="/login" component={Login} />
</Switch>
</div>
</Fragment>
</Router>
</Provider>
</ReduxProvider>
);
}
}
这是我遇到的错误:
Uncaught Error: Objects are not valid as a React child (found: object with keys {$$typeof, render}). If you meant to render a collection of children, use an array instead.
答案 0 :(得分:0)
我忘记了withAlerts函数调用的括号:
export default connect(mapStateToProps)(withAlert()(Alerts));
答案 1 :(得分:0)
我们使用
export default connect(mapStateToProps)(withAlert(Alerts));
使用较早版本的警报模块,因此您无法尝试如下所示
import { withAlert } from "react-alert";
import {connect} from "react-redux";
import PropTypes from "prop-types";
class Alerts extends Component {
static propTypes = {
error: PropTypes.object.isRequired,
message: PropTypes.object.isRequired
};
componentDidUpdate(prevProps) {
const { error, alert, message } = this.props;
if (error !== prevProps.error) {
if (error.msg.name) alert.error(`Name: ${error.msg.name.join()}`);
if (error.msg.email) alert.error(`Email: ${error.msg.email.join()}`);
if (error.msg.message)
alert.error(`Message: ${error.msg.message.join()}`);
if (error.msg.non_field_errors)
alert.error(error.msg.non_field_errors.join());
if (error.msg.username) alert.error(error.msg.username.join());
}
if (message !== prevProps.message) {
if (message.deleteItem) alert.success(message.deleteItem);
if (message.addItem) alert.success(message.addItem);
if (message.passwordNotMatch) alert.error(message.passwordNotMatch);
}
}
render() {
return <Fragment />;
}
}
const mapStateToProps = state => ({
error: state.errors,
message: state.messages
});
export default connect(mapStateToProps)(withAlert()(Alerts));