Group Chat Week 7 Solutions

Well done if you got these right!

  • How do we get the count to update when we click the button in this code?

    • We need to add an onClick prop to the button here. Every time the button gets clicked, we can update the count accordingly by using the setCount function, passing in the value of count + 1.
  • How many console logs will show up in the console from this code?

    • None! This is a trick question! We’re only making a console log if firstLoad is set to false. Since the dependency array is empty, this means that we’re only running this effect once, so by the time that firstLoad is set to false, we won’t run that code again.
  • How do we make the count on the screen come from the context API in this code?

    • We can do this by replacing const [count, setCount] = React.useState(0) with const { count, setCount } = React.useContext(CountContext)
  • What is wrong with the code in App.js in this code? Note: redirecting won’t work on playcode.io!

    • On line 25 (<Route path="/">), we aren’t including the exact prop, which says that we only want to render the “/” route, and not also the “/about” route. This means that, if we were to visit the “/about” route, we would get both the homepage AND the about page showing up!
    • We can fix this by changing this line to <Route exact path="/">.