npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@elao/react-native-realm-connect

v1.0.4

Published

Connect React Native components to Realm queries

Downloads

4

Readme

React Native Realm Connect

Connect React Native components to Realm queries:

  • Map a proptype to a Realm query quite like you would map Redux store state to props with Redux connect.
  • Every time the Realm query changes, your component will receive the updated props (and thus re-render).

Installation

npm install @elao/react-native-realm-connect

Usage

Get the connector method:

import connectToQuery from '@elao/react-native-realm-connect';

The connectToQuery method is similar to the connect method from redux (but not exactly the same) and should be used as follow:

connectToQuery({
    [prop name]: [function that returns a Realm Results object],
    [...other props]
})(MyWrappedComponent);

List

Example:

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { FlatList } from 'react-native';
import connectToQuery from '@elao/react-native-realm-connect';
import Dog from './model/Dog';

class DogList extends Component {
  static propTypes = {
    dogs: PropTypes.arrayOf(PropTypes.instanceOf(Dog)).isRequired,
  };

  render() {
    const { dogs } = this.props;

    return <FlatList data={dogs} /*...*/ />;
  }
}

export default connectToQuery({
  dogs: () => realm.objects('Dog').filtered('goodBoy == $0', true),
})(DogList);

Unique result

Get the unique filter:

import connectToQuery, { unique } from '@elao/react-native-realm-connect';

Mark the query function as unique so that it only returns the first result of the Realm Results object:

connectToQuery({
    [prop name]: unique([function that returns a Realm Results object]),
    [...other props]
})(MyWrappedComponent)

Example:

// ...
import connectToQuery, { unique } from '@elao/react-native-realm-connect';

class MyCat extends Component {
  static propTypes = {
    userId: PropTypes.string.isRequired,
    cat: PropTypes.instanceOf(Cat).isRequired,
  };

  render() {
    const { cat } = this.props;

    return <Text>{cat.name}</Text>;
  }
}

export default connectToQuery({
  cat: unique(props => realm.objects('Cat').filtered('user.id == $0', props.userId)),
})(MyCat);

Extract result

Get the extract filter:

import connectToQuery, { extract } from '@elao/react-native-realm-connect';

You can use extract for any other need, just provide a callback that will be applied on the results object and its return value will be passed as props.

connectToQuery({
    [prop name]: extract([function that returns a Realm Results object with (props)], [callback function with (results, props) parameters],
    [...other props]
})(MyWrappedComponent)

Example:

// ...
import connectToQuery, { unique } from '@elao/react-native-realm-connect';

class MyCat extends Component {
  static propTypes = {
    userId: PropTypes.string.isRequired,
    age: PropTypes.number.isRequired,
  };

  render() {
    const { age } = this.props;

    return <Text>I'm {age} years old.</Text>;
  }
}

export default connectToQuery({
  age: extract(
    props => realm.objects('User').filtered('id == $0', props.userId),
    (results, props) => results ? results[0].age : null,
  ),
})(MyCat);

Usage with Redux

You can use a prop that comes from Redux state and use it in your Realm Query by chainning the two connectors:

//...
import { connect } from 'react-redux';
import connectToQuery from '@elao/react-native-realm-connect';

class FishBowl extends Component {
  static propTypes = {
    fishes: PropTypes.arrayOf(PropTypes.instanceOf(Fish)).isRequired,
  };

  render() {
    const { fishes } = this.props;

    return <FlatList data={fishes} /*...*/ />;
  }
}

const mapStateToProps = state => ({
    color: state.filter.fishColor,
});

const RealmConnectedFishBowl = connectToQuery({
  dogs: props => realm.objects('Fish').filtered('color == $0', props.color),
})(FishBowl)

export default connect(mapStateToProps)(RealmConnectedFishBowl);