Git Product home page Git Product logo

react-native-meteor's Introduction

@ajaybhatia/react-native-meteor

Meteor-like methods for React Native.

What is it for ?

The purpose of this library is :

  • To set up and maintain a ddp connection with a ddp server, freeing the developer from having to do it on their own.
  • Be fully compatible with react-native and help react-native developers.
  • To match with Meteor documentation used with React.

Install

yarn add @ajaybhatia/react-native-meteor

or

npm i --save @ajaybhatia/react-native-meteor

!! See detailed installation guide

Warning < RN 0.57.8 Android bug

There was a bug in the react native websocket android implementation that meant the close event wasn't being received from the server. Therefore RN versions prior to React-native 0.57.8 will not detect users being logged out from the server side. There could also be other bugs resulting from this.

Example usage (with withTracker)

import React from 'react';
import { View, Text, FlatList } from 'react-native';
import Meteor, { withTracker } from '@ajaybhatia/react-native-meteor';

Meteor.connect('ws://192.168.X.X:3000/websocket'); //do this only once

const App = ({ settings, todos, todosReady }) => {
  return (
    <View>
      <Text>{settings.title}</Text>
      {!todosReady && <Text>Not ready</Text>}

      <FlatList
        data={todos}
        keyExtractor={item => item._id}
        renderItem={({ item }) => <Text>{item.title}</Text>}
      />
    </View>
  );
};

export default withTracker(params => {
  const handle = Meteor.subscribe('todos');
  Meteor.subscribe('settings');

  return {
    todosReady: handle.ready(),
    todos: Meteor.collection('todos').find({}, { sort: { createdAt: -1 } }),
    settings: Meteor.collection('settings').findOne(),
  };
})(App);

Example usage (with useTracker hook)

import React from 'react';
import { View, Text, FlatList } from 'react-native';
import Meteor, { useTracker } from '@ajaybhatia/react-native-meteor';

Meteor.connect('ws://192.168.X.X:3000/websocket'); //do this only once

export default App = () => {
  const loading = useTracker(() => {
    const handle = Meteor.subscribe('todos');
    Meteor.subscribe('settings');

    return !handle.ready();
  }, []);

  const todos = useTracker(
    () => Meteor.collection('todos').find({}, { sort: { createdAt: -1 } }),
    [loading]
  );
  const settings = useTracker(() => Meteor.collection('settings').findOne(), [
    loading,
  ]);

  if (loading) {
    return (
      <View>
        <Text>Not ready</Text>
      </View>
    );
  }

  return (
    <View>
      {loading && <Text>Not ready</Text>}

      <FlatList
        data={todos}
        keyExtractor={item => item._id}
        renderItem={({ item }) => <Text>{item.title}</Text>}
      />
    </View>
  );
};

Documentation

Author

Want to help ?

Pull Requests and issues reported are welcome! :)

License

react-native-meteor is MIT Licensed.

react-native-meteor's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.