Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 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. if(this.state.sec === this.props.text.length -1) {
  20. clearInterval(this.id);
  21. }
  22. });
  23.  
  24. }, 1000)
  25. }
  26.  
  27. componentWillUnmount() {
  28. clearInterval(this.id);
  29. }
  30.  
  31. render() {
  32. return (
  33. <h1>{this.state.sec}, {this.props.text.substr(0, this.state.sec +1)}</h1>
  34. )
  35. }
  36. }
  37.  
  38. class App extends React.Component {
  39. render() {
  40. return (
  41. <TextTyper text="Witaj!" />
  42. )
  43. }
  44. }
  45.  
  46. document.addEventListener('DOMContentLoaded', function(){
  47. ReactDOM.render(
  48. <App/>,
  49. document.getElementById('app')
  50. );
  51. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement