javascript - Sweet Alert 中使用 JSX 代替 HTML

标签 javascript reactjs jsx sweetalert2

我想在 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?

最佳答案

来自docs

In order to use SweetAlert with JSX syntax, you need to install SweetAlert with React. Note that you need to have both sweetalert and @sweetalert/with-react as dependencies in your package.json.

您需要安装@sweetalert/with-react

npm install @sweetalert/with-react --save

导入

import swal from '@sweetalert/with-react'

和用法

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

Demo

关于javascript - Sweet Alert 中使用 JSX 代替 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56948125/

相关文章:

javascript - 如何创建自己的 babel 处理器替代 `React`

javascript - 如何在 JSX 中重复一个元素?

javascript - 在 React 和 Redux 中重新渲染所有组件

reactjs - 如何在 Material UI 中更改 Select 的文本、图标和下划线颜色

javascript - 与 Redux react : error on propType property

javascript - 有条件地分配对象值?

javascript - AngularJS 和 JSON - 添加子对象

c# - javascript中的复合键表示

javascript - 给定一个函数 A,new A() 和 new A 之间有什么区别吗?

javascript - 在所有数组元素之间添加 'commas' 分隔符,在最后一个数组元素之前添加 'and'