Skip to content

Instantly share code, notes, and snippets.

@yarigpopov
Created October 13, 2021 10:27
Show Gist options
  • Save yarigpopov/d0cdc09b65aa68b76f038e1c83118119 to your computer and use it in GitHub Desktop.
Save yarigpopov/d0cdc09b65aa68b76f038e1c83118119 to your computer and use it in GitHub Desktop.
function App() {
return (
<Router>
<div className="App">
{/* The data provider component responsible
for fetching and managing the data for the child components.
This needs to be at the top level of our component tree.*/}
<DogDataProvider>
<Nav />
<main className="w-full py-5 mx-auto text-center text-white md:py-20 max-w-screen-xl">
<Banner
title={'React Component Patterns:'}
subtitle={'Provider Pattern'}
/>
<Switch>
<Route exact path="/">
{/* A child component that will consume the data from
the data provider component, DogDataProvider. */}
<Profile />
</Route>
<Route path="/friends">
{/* A child component that will consume the data from
the data provider component, DogDataProvider. */}
<DogFriends />
</Route>
</Switch>
</main>
</DogDataProvider>
</div>
</Router>
);
}
const Profile = () => {
// Our custom hook that "subscribes" to the state changes in
// the data provider component, DogDataProvider.
const { data, status, error } = useDogProviderState();
return (
<div>
<h1 className="//...">Profile</h1>
<div className="mt-10">
{/* If the API call returns an error we will show an error message */}
{error ? (
<Error errorMessage={error.message} />
// Show a loading state when we are fetching the data
) : status === Status.loading ? (
<Loader isInherit={true} />
) : (
// Display the content with the data
// provided via the custom hook, useDogProviderState.
<ProfileCard data={data} />
)}
</div>
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment