Last active
July 29, 2019 23:25
-
-
Save lizdenhup/0ae283df2913e5a8a3a914447b9b6b03 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 CandidateList from './Views/CandidateList.js'; | |
import Candidate from './Views/Candidate.js'; | |
import fetch from 'isomorphic-fetch'; | |
import { Route, Switch } from "react-router-dom"; | |
class ParentComponent extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
votes: 0 | |
} | |
this.handleVote = this.handleVote.bind(this); | |
} | |
handleVote(votes) { | |
const nextVoteTotal = votes + 1; | |
this.setState({ | |
votes: nextVoteTotal | |
}) | |
} | |
componentDidMount() { | |
const url = `https://myelection.com` | |
fetch(url, { | |
method: 'GET', | |
headers: { | |
Accept: 'application/json', | |
} | |
}).then((resp) => resp.json()) | |
.then((json) => { | |
this.setState({ votess: json[votes]}) | |
}).catch((error) => | |
console.log(error) | |
) | |
} | |
render() { | |
return ( | |
<div> | |
<Switch> | |
<Route | |
exact path = '/' | |
render={() => | |
<CandidateList | |
votes={this.state.votes} | |
onClick={this.handleVote} | |
/>}/> | |
<Route | |
path='/voting_totals/:candidateId' | |
render={() => <Candidate candidateTotal={this.state.votes} />}/> | |
</Switch> | |
</div> | |
); | |
} | |
} | |
export default ParentComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment