GithubHelp home page GithubHelp logo

devsnaked / react-relay-offline Goto Github PK

View Code? Open in Web Editor NEW

This project forked from morrys/react-relay-offline

0.0 2.0 0.0 89 KB

JavaScript library files for Relay Modern Offline

License: MIT License

TypeScript 100.00%

react-relay-offline's Introduction

React Relay Offline is a extension of Relay for offline capabilities

Installation

Install react-relay and react-relay-offline using yarn or npm:

yarn add react-relay react-relay-offline

React Web Example

The relay-offline-examples repository is a fork of relay-examples and contains an integration of react-relay-offline. To try it out:

git clone https://github.com/morrys/relay-examples.git
cd relay-examples/todo
yarn
yarn build
yarn start

Then, just point your browser at http://localhost:3000.

How to create the environment

import { Network } from 'relay-runtime';
import { OfflineStore, Store, Environment, RecordSource } from 'react-relay-offline';

const network = Network.create(fetchQuery);
const storeOffline = OfflineStore(network);
const source = new RecordSource(storeOffline);
const store = new Store(storeOffline, source);
const modernEnvironment = new Environment({ network, store }, storeOffline);

Change the renderer

import {QueryRenderer} from 'react-relay-offline'; 

React Native Example

How to create the environment

import { Network } from 'relay-runtime';
import { OfflineStore, Store, Environment, RecordSource } from 'react-relay-offline';

const network = Network.create(fetchQuery);
const storeOffline = OfflineStore(network);
const source = new RecordSource(storeOffline);
const store = new Store(storeOffline, source);
const modernEnvironment = new Environment({ network, store }, storeOffline);

Change the renderer

import {QueryRenderer} from 'react-relay-offline'; 

In QueryRenderer you need to set the property LoadingComponent.

IndexedDB

localStorage is used as the default react web persistence, while AsyncStorage is used for react-native.

To use persistence via IndexedDB:

import OfflineStore from 'react-relay-offline/lib/runtime/redux/OfflineStoreIDB'

OfflineStore

It is possible to customize the offline store through these parameters:

  • storage: any
  • keyPrefix: string
  • customWhitelist: string[]
  • serialize: boolean
  • customReducers: ReducersMapObject

QueryRenderer

  • Add "LoadingComponent" property
  • Add "cached" property in render function
  • Add CACHE_FIRST in dataFrom, with this property the query is not executed on the network if it finds valid results in the cache
<QueryRenderer
        environment={environment}
        query={query}
        variables={{}}
        LoadingComponent={<Loading />}
        render={({ props, error, retry, cached }) => {

Hooks

const hooksProps = useQuery(props);

Requirement

  • Version 3.0.0 or 4.0.0 of the react-relay library
  • When a new node is created by mutation the id must be generated in the browser to use it in the optimistic response

TODO

  • Documentation

  • Implementation of Refetch Container Offline (The implementation of the refetchContainer involves the management of fetchPolicy (network-only and store-or-network). To make it homogeneous with the management of the "QueryRenderer" I have to add the cache-first and offline policy), for the moment it is usable only as specified by the Relay library.

  • Create a pull request to the Relay project (i must use flow type)

License

React Relay Offline is MIT licensed.

react-relay-offline's People

Contributors

morrys avatar helielson avatar

Watchers

James Cloos avatar Maximilly Moreira Gonçalves avatar

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.