-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Voidable sub factory, reactive ready, and a Readme
- Loading branch information
Showing
3 changed files
with
77 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,44 @@ | ||
meteor/react-mongo | ||
================== | ||
|
||
``` | ||
meteor add meteor/react-mongo | ||
``` | ||
|
||
A set of hooks for using Meteor's Mini Mongo Collections and pub/sub. | ||
|
||
There are two hooks | ||
|
||
| Hook | Function | ||
| ---- | -------- | ||
| useSubscription | Used to set up a Meteor subscription. In SSR, this hook is responsible for capturing data to be sent to the client for hydration. | ||
| useCursor | Manages the lifecycle of a Mongo Cursor | ||
|
||
Both methods accept a factory method, and deps. | ||
|
||
## useSubscription(factory, deps) | ||
|
||
`useSubscription` takes a factory method, which should return a subscription handle, and a deps array. It can also return `void` to conditionally set up no subscription. The hook returns a subscription handle with a reactive `ready` method. | ||
|
||
Invoking the `ready()` handle method will cause the hook to update react when the subscription becomes available. | ||
|
||
### Example | ||
|
||
```jsx | ||
import React from 'react' | ||
import { useSubscription } from 'meteor/react-mongo' | ||
|
||
const MyComponent = ({ id = null }) => { | ||
const subscription = useSubscription(() => { | ||
if (id) return Meteor.subscribe(id) | ||
}, [id]) | ||
|
||
return <div> | ||
{ | ||
subscription.ready() | ||
? 'content ready' | ||
: 'loading...' | ||
} | ||
</div> | ||
} | ||
``` |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { Meteor } from 'meteor/meteor'; | ||
import { Mongo } from 'meteor/mongo'; | ||
import { DependencyList } from 'react'; | ||
export declare const useSubscription: (factory: () => Meteor.SubscriptionHandle, deps?: DependencyList) => Meteor.SubscriptionHandle; | ||
declare type UseSubscriptionOptions = { | ||
deps?: DependencyList; | ||
updateOnReady?: boolean; | ||
}; | ||
export declare const useSubscription: (factory: () => Meteor.SubscriptionHandle | void, deps?: DependencyList | UseSubscriptionOptions) => void; | ||
export declare const useCursor: <T = any>(factory: () => Mongo.Cursor<T>, deps?: DependencyList) => Mongo.Cursor<T>; | ||
export {}; |