react-native-meteor-hooks

0.0.2 • Public • Published

react-native-meteor-hooks ☄️

React Native 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 Native. All Meteor computations from the hooks are stopped automatically when the component is removed from the tree.

import React from 'react'
import { View, Text } from 'react-native'
import { useCurrentUser } from 'react-native-meteor-hooks'
 
const UserWidget = () => {
  const currentUser = useCurrentUser()
  return(
    <View>
      { currentUser ? <Text>{ currentUser.userName }</Text> : <Text>You are not logged in.</Text> }
    </View>
  )
}
 

Available Hooks

useTracker( Function: trackerFun [, Array: deps] ) : trackerFunResult

Runs a function inside Trackr.autorun and can return reactive data.

import React from 'react'
import { useTracker } from 'react-native-meteor-hooks'
 
const UserBooks = (sortBy) => {
  const data = useTracker(() => {
    const userProfile = Meteor.user().profile
    const userBooks = Meteor.collection("Books").find({ _owner: Meteor.userId() }, { sort: { [sortBy]: -1 }})
    return { userProfile, userBooks }
  }, [sortBy])
  // pass [sortBy] as second arg - so that this function will be rerun if sortBy changes
 
  const books = data.userBooks.map(Book)
  // ...
}
 

useSubscription( String: subscriptionName [, ...subscriptionParams] ) : Boolean

Subscribes to a publication and returns a reactive "loading" var.

import React from 'react'
import { useSubscription } from 'react-native-meteor-hooks'
 
const UserBooks = (sortBy, showLimit) => {
  const loading = useSubscription('user_books', showLimit)
  // subscription will be rerun if showLimit changes
  if (loading) {
    // ...
  }
  // ...
}
 

useMongoFetch( MongoQuery: query [, Array: deps] ) : Array | Object

Fetches a MongoQuery and returns the result.

import React from 'react'
import { useMongoFetch, useSubscription } from 'react-native-meteor-hooks'
 
const UserBooks = (sortBy, showLimit) => {
  const loading = useSubscription('user_books', showLimit)
  if (loading) {
    // ...
  } else {
    const allBooks = useMongoFetch(Books.find({}, { sort: { [sortBy] : -1 }}), [sortBy])
    const books = allBooks.map(/* ... */)
    // ...
  }
}
 

useCurrentUser() : Object | null

Returns the current logged in User or null.

import React from 'react'
import { useCurrentUser, useSubscription } from 'react-native-meteor-hooks'
 
const UserBooks = (sortBy, showLimit) => {
  const user = useCurrentUser()
  if (user) {
    const loading = useSubscription('user_books', showLimit)
    if (loading) {
      // ...
    } else {
      // ...
    }
  }
}
 

Note

This package was inspired by a blog post of ARTHUR ANDERSEN. Go check out his blog!

Package Sidebar

Install

npm i react-native-meteor-hooks

Weekly Downloads

3

Version

0.0.2

License

MIT

Unpacked Size

9.33 kB

Total Files

9

Last publish

Collaborators

  • ajaybhatia