chentsulin/sweetalert-react
关于
Declarative SweetAlert in React
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文sweetalert-react
Declarative SweetAlert in React
Introduction
sweetalert-react is a wrapped sweetalert implementation with declarative React style component APIs. There is a show prop available for toggling alert component's visibility. And onConfirm, onCancel, onClose, onEscapeKey, onOutsideClick props allow you have a fine grained control over alert component events.
Install
$ npm install sweetalert-react
Usage
import React, { Component } from 'react';
import SweetAlert from 'sweetalert-react';
// ...
render() {
return (
<div>
<button onClick={() => this.setState({ show: true })}>Alert</button>
<SweetAlert
show={this.state.show}
title="Demo"
text="SweetAlert in React"
onConfirm={() => this.setState({ show: false })}
/>
</div>
);
}
You should import sweetalert.css from CDN, file, node_modules(sweetalert/dist/sweetalert.css) or wherever method to include this CSS file.
Checkout the full examples here.
Removed Options
- timer: You should use
setTimeoutand passshowas false. - closeOnConfirm: You should pass
showas false viaonConfirm. - closeOnCancel: You should pass
showas false viaonCancel. - allowEscapeKey: You should pass
showas false viaonEscapeKey. - allowOutsideClick: You should pass
showas false viaonOutsideClick.
All other options can be passed as props, see them in Configuare Section in sweetalert document
FAQ
Q: My alert didn't close when 'go back' or 'go forward' in browser
You can listen to history changes and set show: false when it is mounted. See the full example here.
Q: Can I use react component to render HTML as the alert body?
Sure, you can achieve this 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 the full example here. Thanks @ArkadyB for discovering the approach in issue #53.
Relevant Projects
License
MIT © C.T. Lin
探索相关项目
加载中…