react-meteor-hooks ☄️
React Hooks for Meteor. It was never easier to integrate React into the Meteor stack.
Usage
Just import the hooks you need from this module and you are ready to use Meteors reactive data in React. All Meteor computations from the hooks are stopped automatically when the component is removed from the DOM.
const UserWidget = { const currentUser = return <div> currentUser ? <p> currentUseruserName </p> : <p>You are not logged in</p> </div> }
Available Hooks
useTracker( Function: trackerFun [, Array: deps] ) : trackerFunResult
Runs a function inside Tracker.autorun
and can return reactive data.
const UserBooks = { const data = // pass [sortBy] as second arg - so that this function will be rerun if sortBy changes const books = datauserBooks // ...}
useSubscription( String: subscriptionName [, ...subscriptionParams] ) : Boolean
Subscribes to a publication and returns a reactive "loading" var.
const UserBooks = { const loading = // subscription will be rerun if showLimit changes if loading // ... // ...}
useMethod( String: methodName [, {transform}] ) : Object
Returns { isLoading, data, error, call }
object to work with meteor methods.
Full example including useEffect
at https://codesandbox.io/s/kj9zqqyrr
const UserBooks = { const isLoading data error call = if isLoading return 'Adding book...' if error return `Error: ` return <div> <button onClick=async { await console } > Add </button> data && <p>New book added at dataupdatedAt ID: dataid</p> </div> }
useMongoFetch( MongoQuery: query [, Array: deps] ) : Array | Object
Fetches a MongoQuery and returns the result.
const UserBooks = { const loading = if loading // ... else const allBooks = const books = allBooks // ... }
useCurrentUser() : Object | null
Returns the current logged in User or null.
const UserBooks = { const user = if user const loading = if loading // ... else // ... }
useSession(String: sessionName) : result | undefined
Returns value of a Session var.
Session const UserBooks = { const page = // ...}
Note
This package was inspired by a blog post of ARTHUR ANDERSEN. Go check out his blog!