New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@realm.io/react

Package Overview
Dependencies
Maintainers
7
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@realm.io/react

React specific hooks and implementation helpers for Realm

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21
increased by162.5%
Maintainers
7
Weekly downloads
 
Created
Source

Realm React Logo

Realm React

A better way to use Realm with React Native applications.

Introduction

Setting up Realm in a React Native application has historically been complex. Re-rendering of components when objects in the database change requires manually adding and removing listeners, which produce a lot of boilerplate code and is error-prone (if listeners properly removed on unmount). This library alleviates that by providing React hooks which return Realm data that is state aware. As a consequence, any change to the Realm data will cause components using the hook to re-render.

Installation

This library requires react-native >= 0.59 and realm >= 10.0.0

npm:

npm install @realm.io/react

yarn:

yarn add @realm.io/react

Usage

Setup the RealmContext and Provider

Create a Realm context object with createRealmContext. It takes a Realm configuration and returns a RealmProvider and contextual hooks.

// realm.ts
import {createRealmContext} from '@realm.io/react';

class Task extends Realm.Object {
  _id!: Realm.BSON.ObjectId;
  description!: string;
  isComplete!: boolean;

  static generate(description: string) {
    return {
      _id: new Realm.BSON.ObjectId(),
      description,
      isComplete: false,
      createdAt: new Date()
    };
  }

  // To use a class as a Realm object type, define the object schema on the static property "schema".
  static schema = {
    name: 'Task',
    primaryKey: '_id',
    properties: {
      _id: 'objectId',
      description: 'string',
      isComplete: {type: 'bool', default: false},
      createdAt: 'date'
    },
  };
}

export default createRealmContext({schema: [Task]});

Wrap the component needing access to Realm (possibly your entire application) with the RealmProvider componenet. The RealmProvider also accepts Realm configuration properties.

import RealmContext from './realm';

const {RealmProvider} = RealmContext

function AppWrapper() {
  return (
    <RealmProvider>
      <App />
    </RealmProvider>
  );
};

Implement Hooks in Child Components

The hooks created by createRealmContext can now be used by any child component.

import RealmContext from './realm';

const {useRealm, useQuery, useObject} = RealmContext

function MyComponent({someId}){
  const realm = useRealm();
  const tasks = useQuery(Task);
  const someObject = useObject<SomeObject>('Objects', someId);

  // sort collection with useMemo
  const sortedTasks = useMemo( () => tasks.sorted("createdAt"), [tasks])

  // make sure the data is there
  if(!sortedTasks || !someObject){
    return null
  }

  return ...
}

Dynamically Updating a Realm Configuration

It is possible to update the realm configuration by setting props on the RealmProvider. The RealmProvider takes props for all possible realm configuration properties.

For example, one could setup the sync configuration based on a user state:

const [user, setUser] = useState()

//... some logic to get user state

<RealmProvider sync={user, parition}>

Keywords

FAQs

Package last updated on 11 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc