reactjs - 'withAlert' is showing "Objects are not valid as React Child" Error
问题描述
I am using react-alert and I am getting an error which says:
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.
in Unknown (at App.js:24)
in Provider (at App.js:21)
in Provider (at App.js:20)
in App (at src/index.js:12)
in StrictMode (at src/index.js:11) "
The 'Unknown' is my Alerts component which I was testing out to see if it shows errors as an alert.
Alert.js
import React, { Component, Fragment } from "react";
import { withAlert } from "react-alert";
import { connect } from "react-redux";
import PropTypes from "prop-types";
export class Alerts extends Component {
componentDidMount(){
this.props.alert.show("It works")
}
render() {
return (
<Fragment />
);
}
}
export default withAlert(Alerts);
App.js const alertOptions = { timeout: 3000, position: "top center", };
class App extends Component {
render(){
return (
<Provider store={store}>
<AlertProvier template={AlertTemplate} {...alertOptions}>
<Fragment>
<Header />
<Alerts />
<div className="container">
<Dashboard />
</div>
</Fragment>
</AlertProvier>
</Provider>
);
}
}
export default App;
When I remove the 'withAlert' from "export default withAlert(Alerts)" inside my Alerts.js, the errors are gone but obviously, I can't see the errors. What shall I do to fix this and use react-alert properly? Thanks!
解决方案
Replace this:
withAlert(Alerts)
by this
withAlert()(Alerts)
推荐阅读
- python - 循环遍历不同的 python 字典 - 错误的结果?
- javascript - 如何同时使用fabric.js 和paper.js 制作绘图应用程序?
- autosys - 重新启动失败的子作业
- c++ - 如果 `if constexpr` 不匹配,则停止编译
- vba - 如何拆分字符串以将每个单个数字乘以 ms access 2007 中的值
- javascript - 为什么我在“消除渲染阻塞资源”后获得较低的 PageSpeed 分数?
- spring - NGINX - Spring Boot 应用程序(“/etc/nginx/html/index.html”未找到)
- git - 有没有办法推送评论而不是代码?
- typescript - Typescript:返回给定类型函数的类型
- android - 使用 Azure Pipelines 条件更改传递给 GradleTask 的任务命令