Declarative SweetAlert in React
sweetalert-react is a wrapped sweetalert implement with declarative React style component api. There is a show prop on it to determinate that alert should be displayed or not, and onConfirm, onCancel, onClose, onEscapeKey and onOutsideClick props to have more controls on alert element event.
$ npm install sweetalert-react
import React, { Component } from 'react';
import SweetAlert from 'sweetalert-react';
// ...
render() {
return (
<button onClick={() => this.setState({ show: true })}>Alert</button>
<SweetAlert
show={this.state.show}
title="Demo"
text="SweetAlert in React"
onConfirm={() => this.setState({ show: false })}
/>
);
}
You should import sweetalert.css from cdn, file, node_modules(sweetalert/dist/sweetalert.css) or wherever can find the css code.
Checkout full examples here.
setTimeout and pass show as false.show as false via onConfirm.show as false via onCancel.show as false via onEscapeKey.show as false via onOutsideClick.All of other options can be passed as props, see them in Configuare Section in sweetalert document
You can listen history change and set show: false when it mounted. See full example here.
Sure, you can achieve it with ReactDOMServer.renderToStaticMarkup:
import { renderToStaticMarkup } from 'react-dom/server';
<SweetAlert
show={this.state.show}
title="Demo"
html
text={renderToStaticMarkup(<HelloWorld />)}
onConfirm={() => this.setState({ show: false })}
/>
See full example here. Thanks @ArkadyB for discovering the approach in issue #53.
MIT © C.T. Lin
$ claude mcp add sweetalert-react \
-- python -m otcore.mcp_server <graph>