Task 3: Update App.jsx
In this step, you will create the following:
- A variable to manage the state of our project
- A variable to hold the unique values of the
orbitType
property of the satellite objects - A function that will filter through the satellites.
These variables and this function will be used by the Buttons
and Table
components.
Inside the App.jsx
file
Open the App.jsx
file.
Import
useState
from"react"
.Import
satData.jsx
assatData
.Inside the
App()
function create the first variable.7 8 9
function App() { const [sat, setSat] = useState(satData); // code continues ...
This array holds two variables:
sat
andsetSat
.sat
will be used to compare changes in state.setSat
is a function that will be used to update the state. We set this array equal to theuseState
function.useState
is passedsatData
. You will use these in the other components.Next, create the second variable, called
displaySats
.7 8 9 10
function App() { const [sat, setSat] = useState(satData); const displaySats = [...new Set(satData.map((data) => data.orbitType))]; // code continues ...
This variable will use the
Set
method to create a new array that holds unique elements. This function will be used to create the buttons, so we want to pull out theorbitType
values of each object insidesatData
. TheSet
method prevents duplicate elements. We are using it because there are only three types oforbitTypes
: Low, Medium, and High. This will eventually create three buttons. If we only used map, we would create 10 buttons, and many of them would be duplicates.Next, create an arrow function called
filterByType
.- This function will take a parameter that you can call
currentType
. - Inside this function, you will update the
displaySats
variable using thefilter
method .filter
is similar tomap
, in that it will iterate through a collection until it finds the desired element.
filter
requires a callback, call the callbacknewSatDisplay
.- The
filter
function will return thenewSatDisplay.orbitType
that is equal tocurrentType
. - Close the
filter
function’s return statement. - Before closing the arrow function, we want to update
setSat
by passing it the newly updateddisplaySats
.
11 12 13 14 15 16
const filterByType = (currentType) => { const displaySats = satData.filter((newSatDisplay) => { return newSatDisplay.orbitType === currentType; }); setSat(displaySats); };
- This function will take a parameter that you can call
Before we leave the
App()
function, we need to provide props for theButtons
andTable
components.11 12 13 14 15 16 17 18 19
<> <Banner /> <Buttons filterByType={filterByType} setSat={setSat} displaySats={displaySats} /> <Table sat={sat} /> </>