Skip to content

Instantly share code, notes, and snippets.

@CaptainN
Last active January 28, 2020 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaptainN/4b54cb117af123a5dca122acba77ec08 to your computer and use it in GitHub Desktop.
Save CaptainN/4b54cb117af123a5dca122acba77ec08 to your computer and use it in GitHub Desktop.
Meteor Prefab Hook
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