【发布时间】:2021-06-30 00:57:35
【问题描述】:
我按照 react-notifications-component 的 NPM 页面上的步骤操作,但我不断收到“TypeError:this.add is not a function”。
步骤:
- 使用
npm install react-notifications-component安装。 - 使用
npm install animate.css安装 animate.css。 - 导入 react-notifications-component 和
store。
import ReactNotification, { store } from "react-notifications-component";
import "react-notifications-component/dist/theme.css";
import "animate.css/animate.min.css";
- 在我的 App.js 渲染中包含
<ReactNotification />。
function App() {
return (
<div className="App">
<ReactNotification />
</div>
);
}
- 然后在返回 JSX 之前调用
store.addNotification(),与 NPM 页面上写的完全一样。
store.addNotification({
title: "Wonderful!",
message: "teodosii@react-notifications-component",
type: "success",
insert: "top",
container: "top-right",
animationIn: ["animate__animated", "animate__fadeIn"],
animationOut: ["animate__animated", "animate__fadeOut"],
dismiss: {
duration: 5000,
onScreen: true
}
});
但是,我只是不断收到TypeError: this.add is not a function
TypeError: this.add is not a function
Store.addNotification
src/store/index.ts:49
46 | this.types,
47 | this.defaultNotificationWidth
48 | );
> 49 | return this.add(parsedNotification);
| ^ 50 | }
51 |
52 | public getCounter = () => this.counter;
View compiled
这一切都是在运行 npx create-react-app 之后在一个全新的 repo 中完成的。知道我可能做错了什么吗?
【问题讨论】:
-
@lawrence-witt 谢谢,src/store/index.ts 来自
react-notification-component库,完整文件可以从这里的repo 获得:github.com/teodosii/react-notifications-component/blob/master/… -
我遇到了同样的问题。你设法解决了这个问题吗?
-
@Marco 不幸的是没有,但我切换到react-notifications 这基本上是一样的。
标签: reactjs