【问题标题】:JSX instead of HTML in Sweet Alert甜蜜警报中的 JSX 而不是 HTML
【发布时间】:2019-07-09 08:12:28
【问题描述】:

我想像这样在 Sweet Alert 中编写 JSX:

class TodoApp extends React.Component { 
  componentDidMount() {
    swal({
      html: <Welcome />
    });
  };

  render() {
    return <Welcome />;
  }
}

const Welcome = (props) => <p>Welcome to Sweet Alert and React</p>;

ReactDOM.render(<TodoApp />, document.querySelector("#app"))

但我收到Uncaught TypeError: t.html.cloneNode is not a function 错误。

如何在 Sweet Alert html 中编写 JSX?

【问题讨论】:

  • 重写 Sweet Alert 中处理该数据的部分
  • @Quentin 但是如何?

标签: javascript reactjs jsx sweetalert2


【解决方案1】:

来自docs

为了使用带有 JSX 语法的 SweetAlert,您需要安装带有 React 的 SweetAlert。请注意,您需要在 package.json 中同时拥有 sweetalert 和 @sweetalert/with-react 作为依赖项。

你需要安装@sweetalert/with-react

npm install @sweetalert/with-react --save

导入

import swal from '@sweetalert/with-react'

和用法

swal(<Welcome />) //No need of `html` key

Demo

【讨论】:

  • 由于新版本的 React 不支持,React > v17 还有其他类似的解决方案吗?
猜你喜欢
  • 2018-09-29
  • 1970-01-01
  • 2018-07-25
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多