Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4. class TextTyper extends React.Component {
  5. constructor(props) {
  6. super(props);
  7.  
  8. this.state = {
  9. sec: 0,
  10. }
  11. }
  12.  
  13. componentDidMount() {
  14. this.id = setInterval(() => {
  15.  
  16. this.setState({
  17. sec: this.state.sec + 1,
  18. }, () => {
  19. let stop = this.props.text.length -1;
  20. if(this.props.reversed === true) {
  21. stop += 1;
  22. }
  23.  
  24. if(this.state.sec === stop) {
  25. clearInterval(this.id);
  26. }
  27. });
  28.  
  29. }, 1000)
  30. }
  31.  
  32. componentWillUnmount() {
  33. clearInterval(this.id);
  34. }
  35.  
  36. render() {
  37. if(this.props.reversed === true) {
  38. return (
  39. <h1>
  40. {this.state.sec},
  41. {this.props.text.substr(0, this.props.text.length -this.state.sec)}
  42. </h1>
  43. )
  44. }
  45.  
  46. return (
  47. <h1>{this.state.sec}, {this.props.text.substr(0, this.state.sec +1)}</h1>
  48. )
  49. }
  50. }
  51.  
  52. class App extends React.Component {
  53. render() {
  54. return (
  55. <div>
  56. <TextTyper text="Witaj!" />
  57. <TextTyper text="Witaj!" reversed={true} />
  58. </div>
  59. )
  60. }
  61. }
  62.  
  63. document.addEventListener('DOMContentLoaded', function(){
  64. ReactDOM.render(
  65. <App/>,
  66. document.getElementById('app')
  67. );
  68. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement