Hi I'm doing a course to learn React Hooks and i'm stuck on a task. My task is to create a button that onClick displays a random quote from this array.
const App = (props) => {
const [selected, setSelected] = useState(0)
return (
<div>
{props.anecdotes[selected]}
</div>
)
}
const anecdotes = [
'If it hurts, do it more often',
'Adding manpower to a late software project makes it later!',
'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.',
'Any fool can write code that a computer can understand. Good programmers write code that humans can understand.',
'Premature optimization is the root of all evil.',
'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.'
]
ReactDOM.render(
<App anecdotes={anecdotes} />,
document.getElementById('root')
)
I have tried to solve this myself however I encounter errors. Wondered if someone had an easy explanation for this? Thanks
This is the error I keep on receiving.
TypeError: Cannot read property '0' of undefined App src/App.js:14 11 | 12 | return ( 13 |
14 | Get Random | ^ 15 | {anecdotes[selected]} 16 | 17 | )