Skip to content

Instantly share code, notes, and snippets.

@lizdenhup
Last active July 29, 2019 23:25
Show Gist options
  • Save lizdenhup/0ae283df2913e5a8a3a914447b9b6b03 to your computer and use it in GitHub Desktop.
Save lizdenhup/0ae283df2913e5a8a3a914447b9b6b03 to your computer and use it in GitHub Desktop.
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