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
orbitTypeproperty 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
useStatefrom"react".Import
satData.jsxassatData.Inside the
App()function create the first variable.7 8 9function App() { const [sat, setSat] = useState(satData); // code continues ...This array holds two variables:
satandsetSat.satwill be used to compare changes in state.setSatis a function that will be used to update the state. We set this array equal to theuseStatefunction.useStateis passedsatData. You will use these in the other components.Next, create the second variable, called
displaySats.7 8 9 10function App() { const [sat, setSat] = useState(satData); const displaySats = [...new Set(satData.map((data) => data.orbitType))]; // code continues ...This variable will use the
Setmethod to create a new array that holds unique elements. This function will be used to create the buttons, so we want to pull out theorbitTypevalues of each object insidesatData. TheSetmethod 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
displaySatsvariable using thefiltermethod .filteris similar tomap, in that it will iterate through a collection until it finds the desired element.
filterrequires a callback, call the callbacknewSatDisplay.- The
filterfunction will return thenewSatDisplay.orbitTypethat is equal tocurrentType. - Close the
filterfunction’s return statement. - Before closing the arrow function, we want to update
setSatby passing it the newly updateddisplaySats.
11 12 13 14 15 16const 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 theButtonsandTablecomponents.11 12 13 14 15 16 17 18 19<> <Banner /> <Buttons filterByType={filterByType} setSat={setSat} displaySats={displaySats} /> <Table sat={sat} /> </>