Alerts

Alerts - 图1

  1. import React from 'react';
  2. import { Alert } from 'reactstrap';
  3. const Example = (props) => {
  4. return (
  5. <div>
  6. <Alert color="primary">
  7. This is a primary alert check it out!
  8. </Alert>
  9. <Alert color="secondary">
  10. This is a secondary alert check it out!
  11. </Alert>
  12. <Alert color="success">
  13. This is a success alert check it out!
  14. </Alert>
  15. <Alert color="danger">
  16. This is a danger alert check it out!
  17. </Alert>
  18. <Alert color="warning">
  19. This is a warning alert check it out!
  20. </Alert>
  21. <Alert color="info">
  22. This is a info alert check it out!
  23. </Alert>
  24. <Alert color="light">
  25. This is a light alert check it out!
  26. </Alert>
  27. <Alert color="dark">
  28. This is a dark alert check it out!
  29. </Alert>
  30. </div>
  31. );
  32. };
  33. export default Example;

Properties

  1. Alert.propTypes = {
  2. className: PropTypes.string,
  3. closeClassName: PropTypes.string,
  4. color: PropTypes.string, // default: 'success'
  5. isOpen: PropTypes.bool, // default: true
  6. toggle: PropTypes.func,
  7. tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
  8. fade: PropTypes.bool, // default: true
  9. // Controls the transition of the alert fading in and out
  10. // See Fade for more details
  11. transition: PropTypes.shape(Fade.propTypes),
  12. }

Alerts - 图2

  1. import React from 'react';
  2. import { Alert } from 'reactstrap';
  3. const Example = (props) => {
  4. return (
  5. <div>
  6. <Alert color="primary">
  7. This is a primary alert with <a href="#" className="alert-link">an example link</a>. Give it a click if you like.
  8. </Alert>
  9. <Alert color="secondary">
  10. This is a secondary alert with <a href="#" className="alert-link">an example link</a>. Give it a click if you like.
  11. </Alert>
  12. <Alert color="success">
  13. This is a success alert with <a href="#" className="alert-link">an example link</a>. Give it a click if you like.
  14. </Alert>
  15. <Alert color="danger">
  16. This is a danger alert with <a href="#" className="alert-link">an example link</a>. Give it a click if you like.
  17. </Alert>
  18. <Alert color="warning">
  19. This is a warning alert with <a href="#" className="alert-link">an example link</a>. Give it a click if you like.
  20. </Alert>
  21. <Alert color="info">
  22. This is a info alert with <a href="#" className="alert-link">an example link</a>. Give it a click if you like.
  23. </Alert>
  24. <Alert color="light">
  25. This is a light alert with <a href="#" className="alert-link">an example link</a>. Give it a click if you like.
  26. </Alert>
  27. <Alert color="dark">
  28. This is a dark alert with <a href="#" className="alert-link">an example link</a>. Give it a click if you like.
  29. </Alert>
  30. </div>
  31. );
  32. };
  33. export default Example;

Additional content

Alerts - 图3

  1. import React from 'react';
  2. import { Alert } from 'reactstrap';
  3. const Example = (props) => {
  4. return (
  5. <div>
  6. <Alert color="success">
  7. <h4 className="alert-heading">Well done!</h4>
  8. <p>
  9. Aww yeah, you successfully read this important alert message. This example text is going
  10. to run a bit longer so that you can see how spacing within an alert works with this kind
  11. of content.
  12. </p>
  13. <hr />
  14. <p className="mb-0">
  15. Whenever you need to, be sure to use margin utilities to keep things nice and tidy.
  16. </p>
  17. </Alert>
  18. </div>
  19. );
  20. };
  21. export default Example;

Dismissing

Alerts - 图4

  1. import React from 'react';
  2. import { Alert } from 'reactstrap';
  3. class AlertExample extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. visible: true
  8. };
  9. this.onDismiss = this.onDismiss.bind(this);
  10. }
  11. onDismiss() {
  12. this.setState({ visible: false });
  13. }
  14. render() {
  15. return (
  16. <Alert color="info" isOpen={this.state.visible} toggle={this.onDismiss}>
  17. I am an alert and I can be dismissed!
  18. </Alert>
  19. );
  20. }
  21. }
  22. export default AlertExample;

Uncontrolled [disable] Alerts

For the most basic use-case an uncontrolled component can provide the functionality wanted without the need to manage/control the state of the component. UncontrolledAlert does not require isOpen nor toggle props to work.

Alerts - 图5

  1. import React from 'react';
  2. import { UncontrolledAlert } from 'reactstrap';
  3. function AlertExample() {
  4. return (
  5. <UncontrolledAlert color="info">
  6. I am an alert and I can be dismissed!
  7. </UncontrolledAlert>
  8. );
  9. }
  10. export default AlertExample;

Alerts without fade

Fade can be disabled using fade=false.

Alerts - 图6

Alerts - 图7

  1. import React from 'react';
  2. import { UncontrolledAlert } from 'reactstrap';
  3. import Alert from '../../../src/Alert';
  4. export class AlertFadelessExample extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. visible: true
  9. };
  10. this.onDismiss = this.onDismiss.bind(this);
  11. }
  12. onDismiss() {
  13. this.setState({ visible: false });
  14. }
  15. render() {
  16. return (
  17. <div>
  18. <Alert color="primary" isOpen={this.state.visible} toggle={this.onDismiss} fade={false}>
  19. I am a primary alert and I can be dismissed without animating!
  20. </Alert>
  21. </div>
  22. );
  23. }
  24. }
  25. export function UncontrolledAlertFadelessExample() {
  26. return (
  27. <div>
  28. <UncontrolledAlert color="info" fade={false}>
  29. I am an alert and I can be dismissed without animating!
  30. </UncontrolledAlert>
  31. </div>
  32. );
  33. }