Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import React, { useState } from 'react'
  2. import ReactDOM from 'react-dom'
  3.  
  4. const App = (props) => {
  5. const [selected, setSelected] = useState(0)
  6. const [votes, setVotes] = useState({1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0})
  7.  
  8. const addVotes = () => {
  9. const newVotes = {
  10. ...votes,
  11. votes[selected]: votes[selected] + 1
  12. }
  13. setVotes(newVotes)
  14. console.log(newVotes)
  15. console.log(votes[selected]
  16. }
  17.  
  18. return (
  19. <div>
  20. <h1>Anecdote of the day</h1>
  21. {props.anecdotes[selected]}
  22. <br />
  23. has {votes[selected]} votes
  24. <br />
  25. <Button handleClick={() => addVotes()} text="vote" />
  26. <Button handleClick={() => setSelected(Math.floor(Math.random() * 6))} text="next anecdote" />
  27. <br />
  28. <h1>Anecdote with most votes</h1>
  29. </div>
  30. )
  31. }
  32.  
  33.  
  34. const Button = (props) => {
  35. return (
  36. <button onClick={props.handleClick}>{props.text}</button>
  37. )
  38. }
  39.  
  40.  
  41.  
  42. const anecdotes = [
  43. 'If it hurts, do it more often',
  44. 'Adding manpower to a late software project makes it later!',
  45. 'The first 90 percent of the code accounts for the first 90 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time.',
  46. 'Any fool can write code that a computer can understand. Good programmers write code that humans can understand.',
  47. 'Premature optimization is the root of all evil.',
  48. 'Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.'
  49. ]
  50.  
  51. ReactDOM.render(
  52. <App anecdotes={anecdotes} />,
  53. document.getElementById('root')
  54. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement