Created
September 30, 2019 11:37
-
-
Save MustafaAnasKH99/92a3bd74437601dafa8702e2c11f56f0 to your computer and use it in GitHub Desktop.
A react component that randomly choose a word and displays it in a sentence from a provided array of strings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
class Toggler extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
do: 'Change default value' | |
}; | |
} | |
tick() { | |
console.log(this.props.list) | |
const { list } = this.props | |
var item = list[Math.floor(Math.random()*list.length)]; | |
this.setState({ | |
do: item, | |
}); | |
} | |
componentDidMount() { | |
const { seconds } = this.props | |
this.interval = setInterval(() => this.tick(), seconds*1000); | |
} | |
componentWillUnmount() { | |
clearInterval(this.interval); | |
} | |
render() { | |
return ( | |
<div> | |
I'm a <code>{this.state.do}</code> developer. | |
</div> | |
); | |
} | |
} | |
export default Toggler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
const {list, seconds} = this.props