Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. import React, { useState } from 'react'
  2. import ReactDOM from 'react-dom'
  3.  
  4. const Button = ({ onClick, text }) => (
  5. <button onClick={onClick}>
  6. {text}
  7. </button>
  8. )
  9. const Voted = (props) => {
  10. console.log(props)
  11. return (
  12. <p>has {props} votes</p>
  13. )
  14.  
  15. }
  16.  
  17.  
  18. const App = (props) => {
  19. const Rnd = () => Math.floor(Math.random()* anecdotes.length)
  20. const [selected, setSelected] = useState(0)
  21. const [vote, setVote] = useState([0,0,0,0,0,0])
  22. const handleVoteClick = () => {
  23. const voteList = [...vote]
  24. voteList[selected] += 1
  25. setVote(voteList)
  26. console.log(voteList)
  27.  
  28. }
  29. return (
  30. <div>
  31.  
  32. <h1> Anecdote of the day </h1>
  33. <div>
  34. {props.anecdotes[selected]}
  35.  
  36.  
  37. </div>
  38. <Voted props = {vote[selected]} />
  39. <Button onClick={handleVoteClick} text = 'vote'
  40.  
  41. />
  42. <Button
  43. onClick={() => setSelected(Rnd)}
  44. text='next anecdote'
  45. />
  46. <h1>Anectode with most votes</h1>
  47. </div>
  48.  
  49. )
  50. }
  51.  
  52. const anecdotes = [
  53. 'If it hurts, do it more often',
  54. 'Adding manpower to a late software project makes it later!',
  55. '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.',
  56. 'Any fool can write code that a computer can understand. Good programmers write code that humans can understand.',
  57. 'Premature optimization is the root of all evil.',
  58. '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.'
  59. ]
  60.  
  61. ReactDOM.render(
  62. <App anecdotes={anecdotes} />,
  63. document.getElementById('root')
  64. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement