Generic Data Containers: Three Levels of Abstraction

Hands-on practice for this lecture. Write the code, see it run, understand the pattern.

1

Exercise 1 of 1

Build the DataSource

Live editor: implement a DataSource container that accepts any getData function, then use it to load both server data and localStorage β€” same component, different sources.

Task

Complete DataSource.js: call getData() in useEffect, store the result, then clone each child and inject { [resourceName]: resource } as props. Then wire up two DataSource uses in App.js: one loading a user object, one reading from localStorage.

Loading editor…

πŸ’‘ Hint: use React.Children.map + React.cloneElement. The same DataSource component works for both the fetch and the localStorage case β€” only the getData function changes.

Practice: Generic Data Containers: Three Levels of Abstraction β€” Interactive Exercises | Durgesh Rai