- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试在我的react/TS应用程序中使用react-toastr。但提供的示例( https://tomchentw.github.io/react-toastr/ )不起作用。
我像这样导入:
import { ToastContainer, ToastMessageAnimated } from "react-toastr";
并像这样使用:
class RegistrationSelectionPage extends React.Component<{}, {}> {
public render() {
let container: ToastContainer | null;
return (
<section className="RegistrationSelectionPage">
<ToastContainer
ref={ref => container = ref}
className="toast-top-right"
toastMessageFactory={ToastMessageAnimated}
/>
<button onClick={() => container && container.warning('No connection', 'Error!')}>Click me</button>
...
我收到此错误:
Uncaught TypeError: Cannot call a class as a function
at ./node_modules/babel-runtime/helpers/classCallCheck.js.exports.default
...
我应该在 ToastMessageAnimated
上调用哪个函数?
请提供完整的工作示例,谢谢:)
最佳答案
@types 不正确,看起来它们已经允许 any
对于 toastMessageFactory
prop 来避免对正确类型的误解,从而导致各种困惑
从文档来看,它需要一个 React 工厂函数 https://tomchentw.github.io/react-toastr/#toastmessage
默认值为React.createFactory(ToastMessageAnimated)
这是从 lib 中定义的 Component 类创建的工厂函数。这确实满足您当前的目的,但当前的定义强制您指定它,并接受 any
这完全违背了这一点,所以让我们看看修复 defs
从默认值我们可以推断出 toastMessageFactory
的类型应该是React.Factory<ToastMessageProps>
这是库中组件类之一的工厂,具有基本的 ToastMessageProps
查找源代码中的 props 和组件(很高兴使用 Flow 编写,因此这变得更加容易)我们可以改进 @type/react-toastr
定义为...
import { Component, ReactHTML, Factory, ReactNode } from 'react'
export interface ToastContainerProps {
toastType?: IconClassNames
id?: string
preventDuplicates?: boolean
toastMessageFactory?: Factory<ToastMessageProps>
newestOnTop?: boolean
onClick?: Function
}
export class ToastContainer extends Component<ToastContainerProps> {
error: <P extends ToastMessageProps>(message: ReactNode, title: ReactNode, optionsOverride?: Partial<P>) => void
info: <P extends ToastMessageProps>(message: ReactNode, title: ReactNode, optionsOverride?: Partial<P>) => void
success: <P extends ToastMessageProps>(message: ReactNode, title: ReactNode, optionsOverride?: Partial<P>) => void
warning: <P extends ToastMessageProps>(message: ReactNode, title: ReactNode, optionsOverride?: Partial<P>) => void
clear: () => void
}
export interface IconClassNames {
error: string
info: string
success: string
warning: string
}
export interface ToastMessageProps {
className?: string,
type: string,
iconClassNames?: IconClassNames,
iconClassName?: string,
closeButton?: boolean,
onCloseClick?: Function,
title?: any,
titleClassName?: string,
message?: any,
messageClassName?: string
}
export interface ToastMessageAnimatedProps extends ToastMessageProps {
className?: string,
showAnimation?: string,
hideAnimation?: string,
timeOut?: number,
extendedTimeOut?: number,
tapToDismiss?: boolean,
onRemove: Function,
onClick?: Function,
onCloseClick?: Function,
}
export interface ToastMessageQueryProps extends ToastMessageProps {
style?: object,
showMethod?: string,
showDuration?: number,
showEasing?: string,
hideMethod?: string,
hideDuration?: number,
hideEasing?: string,
timeOut?: number,
extendedTimeOut?: number,
tapToDismiss?: boolean,
onRemove: Function,
onClick?: Function,
onCloseClick?: Function,
}
export class ToastMessageAnimated extends Component<ToastMessageAnimatedProps> {}
export class ToastMessageQuery extends Component<ToastMessageQueryProps> {}
您现在可以在自己的组件中使用正确键入的这些...
import {
ToastContainer,
ToastMessageAnimated,
ToastMessageAnimatedProps // import this too for stricter typing of factory
} from "react-toastr"
//... all exactly same as your example, except...
<ToastContainer ref={ref => container = ref}
toastMessageFactory={React.createFactory<ToastMessageAnimatedProps>(ToastMessageAnimated)}
/>
<button onClick={() => container && container.warning<ToastMessageAnimatedProps>('No connection', 'Error!', {className: 'toast-top-right'})}>Click me</button>
这里我们还严格输入警告选项,并设置 className
在其中(这被错误地设置为容器 Prop ,因此不会被应用)
现在还允许您将 JSX 组件作为消息和标题传递
container.warning<ToastMessageAnimatedProps>(<div>No connection</div>, <h1>Error!</h1>, {className: 'toast-top-right'})
关于javascript - React-toastr 3 与 typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48185898/
如何在 toastr 中显示多行错误消息? 我已经格式化了我的错误消息,如下所示 "Please fix the following: \r\nFirst Name is required\r\nLa
在 toastr 消息中,我有几个图标在彼此之上。这是它的样子: 我的代码很简单,我在 Ajax 调用后使用 toastr: success : function(reponse) {
有什么办法可以改变吗?我尝试将 toastClass 定义为不透明度设置为 1 的类,但没有看到任何变化: .toast-style{ opacity: 1; } toastr.options.
我正在使用 ngx-toastr图书馆。我正在启动位于屏幕顶部中央的消息,如下所示: @Injectable() export class MessageService { construct
我正在使用 Angular toastr 并且我坚持在隐藏上一个 toastr 和显示下一个 toastr 之间传递延迟。一次应该只有一个 toastr 。我隐藏并显示它,但没有视觉差异,并且 toa
我正在使用 toastr jquery 插件。下面是链接 https://github.com/scottoffen/jquery.toaster 我在这里面临两个问题 目前点击按钮时会显示多个通知(
我正在尝试 toastr,但我不断收到一个奇怪的 TypeError,这对我来说没有任何意义。 TypeError: Cannot read property 'extend' of undefine
我正在使用 angular2-toaster在我的 Angular 应用中 很简单, 在组件模板中定义 toastr 容器 并且您使用 ToasterService 类型的 toasterServi
我想使用toastr来通知用户。我已关注this设置指南。但我收到错误消息:Uncaught TypeError: toastr is not a function。我查看了网络选项卡,文件加载正确。
我正在尝试将 toastr 带入我的应用程序中。我做了一些非常简单的事情: bundles.Add(new ScriptBundle("~/Content/example-scripts")
shared.module.ts import { animate, state, style, transition, trigger } from '@angular/animations'; i
我在 Angular 6 中使用 ngx-toastr 进行 http 错误通知,就像在 httpInterceptor 中注入(inject) ToastrService export cl
我安装了react-toastr并集成了示例代码。 import React from 'react' import { ToastContainer } from 'react-toastr'; c
下面的代码会弹出一个 toastr : function showMessage(code, type) { ConfigService.getErrorMessage(cod
我正在使用 typescript 创建一个非常简单的 Web 应用程序。现在,它要做的就是创建一个矩形并显示该区域的信息提示。不幸的是, toast 从未出现。该代码行已执行,并且 Internet
我喜欢用 toastr 做的事情,但我似乎无法让它发挥作用。 来自官方页面... (1) Link to toastr.css (2) Link to toastr.js (3) Use toastr
从应用程序注销后,我必须显示 toastr 。这就是我正在尝试的。在这里,我在注销方法后调用 toastr 。 _this.logout().then({ success: function (r
我是 Angular js 的新手。我正在尝试显示错误消息 View toaster() .我在谷歌上搜索,但没有找到正确的结果。当我点击 cancel()按钮,我会收到一条错误消息,提示 toast
我正在尝试使用 Toastr 通知 ( https://github.com/CodeSeven/toastr )。它们工作正常,但问题是它们只出现在右上角。我可以更改位置的任何内容,但它们将始终显示
我正在使用 toastr js 插件 - toastr - 并更改了 css 颜色,但是,我不知道如何创建正确的 png/base64 部分? 原文: .toast-info { backgrou
我是一名优秀的程序员,十分优秀!