Created
December 20, 2018 15:50
-
-
Save redcatsec/d807f695fb5a397d21a99917f14be92e to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
import Card from './Card/Card'; | |
import DrawButton from './Drawbutton/DrawButton'; | |
import 'firebase/database' | |
import fb from './config/firebase/db_config' | |
console.log(fb); | |
class App extends Component { | |
constructor(props) { | |
super(props); | |
this.database = fb.database().ref().child('cards'); | |
this.updateCard = this.updateCard.bind(this); | |
this.state = { | |
cards: [ | |
{ | |
id: 2, eng: "play", ger: "spielen" | |
} | |
], | |
currentCard: {} | |
} | |
; | |
} | |
UNSAFE_componentWillMount() { | |
const currentCards = this.state.cards; | |
/**this.database.child('child_added' , snap => { | |
currentCards.push({ | |
id: snap.key, | |
eng : snap.val().eng, | |
ger : snap.val().ger | |
}); | |
});**/ | |
this.setState({ | |
cards: currentCards, | |
currentCard: this.getRandomCard(currentCards) | |
}); | |
} | |
updateCard() { | |
const currentCards = this.state.cards; | |
this.setState({ | |
currentCard: this.getRandomCard(currentCards) | |
}) | |
} | |
getRandomCard(currentCard) { | |
let card = currentCard[Math.floor(Math.random() * currentCard.length)] | |
return card; | |
} | |
render() { | |
return ( | |
<div className="App"> | |
<Card eng={this.state.currentCard.eng} ger={this.state.currentCard.ger} /> | |
<DrawButton drawCard={this.updateCard} /> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment