【问题标题】:How can I display messages in component with react-alert?如何使用 react-alert 在组件中显示消息?
【发布时间】:2019-10-15 19:31:15
【问题描述】:

我正在使用 reactjs。 我想在带有 react-alert 组件的组件中显示警报。 https://www.npmjs.com/package/react-alert 我按照给定的方式包装 index.js 文件。

但是当我尝试在表单中使用alert.show ("sdfsdfsdfsf") 时,出现以下错误。

我是否在表单中显示消息?

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App/App';

import { transitions, positions, Provider as AlertProvider } from 'react-alert'
import AlertTemplate from 'react-alert-template-basic'

import { Provider } from 'react-redux';

const options = {
    // you can also just use 'bottom center'
    position: positions.BOTTOM_CENTER,
    timeout: 5000,
    offset: '30px',
    // you can also just use 'scale'
    transition: transitions.SCALE
};
ReactDOM.render(
    <Provider store={store}>
        <AlertProvider template={AlertTemplate} {...options}>
            <App/>
        </AlertProvider>
    </Provider>,
document.getElementById('root'));

myForm.js

import { useAlert } from "react-alert";
const alert = useAlert();
class myForm extends Component {
  constructor(props) {
    super(props);
.........

render() {
return(
<div> alert.show("Alert test") </div>
)
}

【问题讨论】:

    标签: javascript reactjs react-native alert toast


    【解决方案1】:

    你不能在渲染方法中调用函数/方法,除非它用花括号括起来。 jsx 中需要大括号,让解析器知道您要运行一些 javascript 代码。

    尝试以下方法:

    render() {
       return (
         <div>{alert.show("Alert test")}</div>
       );
    }
    

    npm 上的 react-alert 包在使用 onClick 处理程序时也有一个很好的例子 https://www.npmjs.com/package/react-alert

    const App = () => {
      const alert = useAlert()
    
      return (
        <button
          onClick={() => {
            alert.show('Oh look, an alert!')
          }}
        >
          Show Alert
        </button>
      )
    }
    
    export default App
    

    【讨论】:

    • 类 CustomerDebt 扩展组件 { 构造函数 (props) { super (props);我怎么称呼它?如图所示的错误。
    • 您需要在 return 语句之前将您的 const 警报移动到 render 方法中。 ``` 类 myForm 扩展组件 { 构造函数(道具){ 超级(道具); } render() { 常量警报 = useAlert(); return (
      alert.show("Alert test")
      ) ); }
    • 您正在尝试在类组件中使用挂钩。根据反应文档 (reactjs.org/docs/…),您不能这样做。您将需要更改您的组件以遵循功能组件模式或使用与类一起使用的库版本
    • 您可以使用 HOC 在类组件中使用钩子。参考:stackoverflow.com/questions/57975445/…
    【解决方案2】:

    您正在使用:

    render(){                                                            
      return(
      <div> alert.show("Alert test") </div>                                    
       )
    }
    

    但是您需要使用事件来显示该警报。

    改用这个:

    render(){ 
      return(                                                                
        <div                                                             
          onClick={() => 
            {alert.show('Alert test')}
          }
        />
      )
    }
    

    【讨论】:

    • 挂钩调用无效。 Hooks 只能在函数组件的主体内部调用。这可能是由于以下原因之一: 1. 你可能有不匹配的 React 版本和渲染器(例如 React DOM) 2. 你可能违反了 Hooks 规则 3. 你可能有多个 React 副本相同的应用程序 50 |常量警报 = useAlert(); 51 | 52 |类 CustomerDebt 扩展组件 { 53 |构造函数(道具)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多