Walkthrough: Stateful Components π‘
Solution
1. Import useState()
into the <App />
component
useState()
and other hooks can be imported from "react" package:
2. Add a useState()
hook to your <App />
component with its return value destructured to selectedDate
and setSelectedDate
. Set the initial value of useState()
to the date of the first forecast within forecasts
array.
Assuming that <App />
currently looks like this:
We first need to add a useState() hook before we can add our state. We have to set the selectedDate
property in state to the date of the first forecast from forecasts
:
3. Create a selectedForecast
variable and set it equal to the date within your forecasts
that matches the selectedDate
, and pass this selectedForecast
variable into the <ForecastDetails />
component.
Then we want to pass a forecast
into <ForecastDetails />
based on the selected date:
Now the <ForecastDetails />
component can render whichever forecast matches our selected date. If that date changes, then the forecast rendered in <ForecastDetails />
will also change, but you might see an error in the command line (where you run npm start
) or in the browser:
Don't worry about it now, we will handle that in the next step.