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 ofcount + 1
.
- We need to add an
-
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 tofalse
. Since the dependency array is empty, this means that we’re only running this effect once, so by the time thatfirstLoad
is set tofalse
, we won’t run that code again.
- None! This is a trick question! We’re only making a console log if
-
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)
withconst { count, setCount } = React.useContext(CountContext)
- We can do this by replacing
-
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 theexact
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="/">
.
- On line 25 (