Skip to content

Instantly share code, notes, and snippets.

@MustafaAnasKH99
Created September 30, 2019 11:37
Show Gist options
  • Save MustafaAnasKH99/92a3bd74437601dafa8702e2c11f56f0 to your computer and use it in GitHub Desktop.
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.
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;
@MustafaAnasKH99
Copy link
Author

const {list, seconds} = this.props

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment