Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 [clicks, setClicks] = useState(0)
  7.     const setToSelected = newSelected => {
  8.       setSelected(newSelected)
  9.     }
  10.     const array = Array.apply(null, new Array(6)).map(Number.prototype.valueOf,0);
  11.     const handleClick = () => {
  12.       const newClicks = {
  13.         ...array,
  14.         clicks: array[selected] += 1
  15.       }
  16.       setClicks(newClicks)
  17.     }
  18.     return (
  19.       <div>
  20.         <p>{props.anecdotes[selected]}</p>
  21.         <p>has {clicks} votes</p>
  22.         <button onClick={handleClick}>vote</button>
  23.         <Button handleClick={() => setToSelected(Math.floor(Math.random() * 6))} text="next anecdote" />
  24.       </div>
  25.     )
  26.   }
  27.  
  28.   const Button = (props) => (
  29.     <button onClick={props.handleClick}>
  30.       {props.text}
  31.     </button>
  32.   )
  33.  
  34.  
  35.   const anecdotes = [
  36.     'If it hurts, do it more often',
  37.     'Adding manpower to a late software project makes it later!',
  38.     '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.',
  39.     'Any fool can write code that a computer can understand. Good programmers write code that humans can understand.',
  40.     'Premature optimization is the root of all evil.',
  41.     '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.'
  42.   ]
  43.  
  44.  
  45. ReactDOM.render(
  46.     <App anecdotes={anecdotes} />,
  47.     document.getElementById('root')
  48. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement