Last active
January 28, 2020 20:48
-
-
Save CaptainN/4b54cb117af123a5dca122acba77ec08 to your computer and use it in GitHub Desktop.
Meteor Prefab Hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useTracker } from 'meteor/react-meteor-data' | |
// Create a reusable hook | |
const usePage = (pageId) => useTracker(() => { | |
// The publication must also be secure | |
const subscription = Meteor.subscribe('page', pageId) | |
const page = Pages.findOne({ _id: pageId }) | |
return { | |
page, | |
isLoading: !subscription.ready() | |
} | |
}, [pageId]) | |
const MyProtectedPage = (pageId) => { | |
const { isLoading, page } = usePage(pageId) | |
if (isLoading) { | |
return <div>Loading...</div> | |
} | |
return <div> | |
<h1>{page.title}</h1> | |
<p>{page.content}</p> | |
</div>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment