Created
February 25, 2019 17:33
-
-
Save anttispitkanen/f9e0652d486566daebf97a30a66ca598 to your computer and use it in GitHub Desktop.
Awesome react application, wanna invest?
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"; | |
import "./App.css"; | |
const names = [ | |
"Repe", | |
"Pera", | |
"Kartsa", | |
"Raipe", | |
"Simo", | |
"Jussi", | |
"Kalle", | |
"Tero" | |
]; | |
class App extends Component { | |
state = { | |
people: [{ name: "Antti", age: 28 }, { name: "Vesku", age: 30 }], | |
timer: null | |
}; | |
componentDidMount = () => { | |
this.setState({ | |
timer: window.setInterval(this.addPerson, 1000) | |
}); | |
}; | |
stop = () => { | |
window.clearInterval(this.state.timer); | |
this.setState({ timer: null }); | |
}; | |
generateRandomPerson = () => { | |
return { | |
name: names[Math.floor(Math.random() * names.length)], | |
age: Math.floor(Math.random() * 20) | |
}; | |
}; | |
addPerson = () => { | |
const randomNewPerson = this.generateRandomPerson(); | |
this.setState({ | |
// people: this.state.people.concat(randomNewPerson) | |
people: [randomNewPerson].concat(this.state.people) | |
}); | |
}; | |
render() { | |
console.log(this.state); | |
return ( | |
<div className="App"> | |
<h1>Moimoi</h1> | |
<button onClick={this.addPerson}>Paina tästä!</button> | |
<button onClick={this.stop}>Apua saatana!</button> | |
<div | |
style={{ | |
display: "flex", | |
flexDirection: "row", | |
justifyContent: "center" | |
}} | |
> | |
<ul> | |
<div>Kaikki</div> | |
{this.state.people.map((person, i) => ( | |
<Person name={person.name} age={person.age} key={i} /> | |
))} | |
</ul> | |
<ul> | |
<div>Vaan yli 7-vuotiaat</div> | |
{this.state.people | |
.filter(person => person.age > 7) | |
.map((person, i) => ( | |
<Person name={person.name} age={person.age} key={i} /> | |
))} | |
</ul> | |
<ul> | |
<div>Vaan ne jotka alkaa äRRRällä</div> | |
{this.state.people | |
.filter(person => person.name[0] === "R") | |
.map((person, i) => ( | |
<Person name={person.name} age={person.age} key={i} /> | |
))} | |
</ul> | |
<ul> | |
<div>Vaan yli nelikirjaimiset</div> | |
{this.state.people | |
.filter(person => person.name.length > 4) | |
.map((person, i) => ( | |
<Person name={person.name} age={person.age} key={i} /> | |
))} | |
</ul> | |
</div> | |
</div> | |
); | |
} | |
} | |
export default App; | |
const Person = props => ( | |
<li> | |
{props.name} {props.age} | |
</li> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment