Coding task · JavaScript and React course · Module 15: Patterns and Architecture

Practice: Container/Presentational

The hook holds all the logic (state, loading, computed values) and returns no JSX, while the presentational component gets data and callbacks through props, so it does not know where they come from. Do not keep the selected planet in separate state: find it with the find method from selectedId, so it can never drift away from the list. Thanks to the loadPlanets parameter a test can give the hook its own data instead of the simulated API.

Task

Separate the logic from the view. The usePlanets hook gets a loadPlanets function (by default the simulated API fetchPlanets) and loads the data in useEffect. ___BLANK1___: store the received data in the planets state. ___BLANK2___: selectedPlanet is the planet whose id equals selectedId, computed during rendering from the planets array (null when nothing is selected). PlanetList is a presentational component: ___BLANK3___ calls onSelect with the id of that planet when its button is clicked. ___BLANK4___: App passes PlanetList the function from the hook that changes the selection.