Loading.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React from "react";
  2. const PropTypes = React.PropTypes;
  3. var styles = {
  4. container: {
  5. bottom: 0,
  6. fontSize: "55px",
  7. left: 0,
  8. position: "fixed",
  9. right: 0,
  10. top: 0
  11. },
  12. content: {
  13. marginTop: "30px",
  14. position: "absolute",
  15. textAlign: "center",
  16. width: "100%"
  17. }
  18. };
  19. class Loading extends React.Component {
  20. constructor(props, context, updater) {
  21. super(props, context, updater);
  22. this.originalText = props.text;
  23. this.state = {
  24. text: this.originalText
  25. };
  26. }
  27. componentWillMount() {
  28. const stopper = `${this.originalText}...`;
  29. this.interval = setInterval(() => {
  30. if (this.state.text === stopper) {
  31. // Usually: avoid setState in cDM, because it causes a re-render.
  32. this.setState({
  33. text: this.originalText
  34. });
  35. }
  36. else {
  37. this.setState({
  38. text: `${this.state.text}.`
  39. });
  40. }
  41. }, this.props.speed);
  42. console.log("Loading: cDM", this.interval);
  43. }
  44. componentWillUnmount() {
  45. console.log("Loading: cWU", this.interval);
  46. clearInterval(this.interval);
  47. }
  48. render() {
  49. console.log("Loading: render");
  50. return (
  51. <div style={styles.container}>
  52. <p style={styles.content}>{this.state.text}</p>
  53. </div>
  54. );
  55. }
  56. }
  57. Loading.defaultProps = {
  58. speed: 300,
  59. text: "Loading"
  60. };
  61. Loading.propTypes = {
  62. speed: PropTypes.number,
  63. text: PropTypes.string
  64. };
  65. export default Loading;