All Projects → lolatravel → Realm React Redux

lolatravel / Realm React Redux

Licence: mit
A redux like store with Realm as the state

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Realm React Redux

Redux Micro Frontend
This is a library for using Redux to manage state for self-contained apps in a Micro-Frontend architecture. Each self-contained isolated app can have its own isolated and decoupled Redux store. The componentized stores interact with a global store for enabling cross-application communication.
Stars: ✭ 38 (-47.22%)
Mutual labels:  react-redux
Apollo Redux Form
Redux forms powered by Apollo
Stars: ✭ 52 (-27.78%)
Mutual labels:  react-redux
Choerodon Front
Choerodon Front is a total front-end of Choerodon that combines Choerodon IAM and Choerodon DevOps.
Stars: ✭ 62 (-13.89%)
Mutual labels:  react-redux
Typescript Hapi React Hot Loader Example
Simple TypeScript React Hot Loading example with Hapi Server-side rendering
Stars: ✭ 44 (-38.89%)
Mutual labels:  react-redux
React Todo
ReactJS + CSS Modules + Sass + Blueprint
Stars: ✭ 49 (-31.94%)
Mutual labels:  react-redux
Github View
Analyse activities and contributions of a GitHub user.
Stars: ✭ 54 (-25%)
Mutual labels:  react-redux
Taliesin
Lightweight audio streaming server
Stars: ✭ 35 (-51.39%)
Mutual labels:  react-redux
Dva React Worms
dva新手综合教程
Stars: ✭ 70 (-2.78%)
Mutual labels:  react-redux
Harmoware Vis
Spatial-Temporal Visualization Library using Deck.GL
Stars: ✭ 51 (-29.17%)
Mutual labels:  react-redux
Dva Starter
完美使用 dva react react-router,最好用的ssr脚手架,服务器渲染最佳实践
Stars: ✭ 60 (-16.67%)
Mutual labels:  react-redux
Hapi React Hot Loader Example
Simple React Hot Loading example with Hapi Server-side rendering
Stars: ✭ 44 (-38.89%)
Mutual labels:  react-redux
Redux Connect Decorator
React Redux's connect as a decorator.
Stars: ✭ 46 (-36.11%)
Mutual labels:  react-redux
Egg React Typescript Boilerplate
Egg React TypeScript Server Side Render (SSR) / Client Side Render (CSR)
Stars: ✭ 56 (-22.22%)
Mutual labels:  react-redux
Ts2redux
Compile standard TypeScript classes to Redux or React Context API
Stars: ✭ 39 (-45.83%)
Mutual labels:  react-redux
React Redux Hooks Starter
React-redux boilerplate using hooks 🎣
Stars: ✭ 69 (-4.17%)
Mutual labels:  react-redux
Hellobooks
A Single-Page Library Management App built with nodejs, express and react and redux
Stars: ✭ 37 (-48.61%)
Mutual labels:  react-redux
React Redux Example
React Redux Example
Stars: ✭ 54 (-25%)
Mutual labels:  react-redux
React Realization
some simple Realizations of react
Stars: ✭ 71 (-1.39%)
Mutual labels:  react-redux
Aspnet Starter Kit 2.0
Cross-platform web development with Visual Studio Code, C#, F#, JavaScript, ASP.NET Core, React (ReactJS), Redux, TypeScript. Single-page application boilerplate.
Stars: ✭ 70 (-2.78%)
Mutual labels:  react-redux
Simple Universal React Redux
The simplest possible Async Universal React & Redux Boilerplate app, that works on both Mac and Windows
Stars: ✭ 58 (-19.44%)
Mutual labels:  react-redux

Realm React Redux

Build Status

Goal

The purpose of this project is to provide a reliable, consistent pattern for getting data from the mobile Realm database to React Native components. While there are a few ways this could be done, some of them much simpler, this project specifically follows the pattern of redux and react-redux for a few reasons:

  • redux patterns are familiar to many developers, so following those patterns will make it easier for developers to introduce a persistent Realm store to their applications
  • for projects already using redux (or projects that need both a Realm and redux store), this provides a similar pattern for accessing and updating both stores
  • these patterns have proven to be successful and scale to large and complicated codebases

Status

This project is currently experimental. It is being used in a couple of views in production here at lola and is working quite well, but the API is still likely to change and there may still be bugs in the more advanced use cases. If you are willing to experiment, please report any feedback or issues you encounter.

Comparison to redux and react-redux

The store

A Realm store has the exact same API as a redux store, which means you can write and install middlewares the exact same way. Some redux middlewares may even work directly with the Realm store if they only inspect actions and do not rely on the state. With a Realm store, the store.getState() method returns a Realm instance instead of a state object.

Actions

Actions work the exact same way as they do in redux, and you can technically dispatch the same actions to both a redux store or a Realm store, although in practice it may be better to keep them separate.

Writers vs. Reducers

In redux you have a reducer that takes the current state and an action which it processes to return a new state. In realm-react-redux there is a similar concept: the writer. A writer takes a Realm instance and an action and writes to the Realm db. The store handles wrapping the entire thing in a single realm.write() transaction so your writers can focus on handling only creates/updates/deletes it needs. There is also a combineWriters() method which takes an array of writers and combines them into a single one for the store, very similar to combineReducers() in redux.

Provider

react-redux was designed with extensibility in mind so we can actually use its Provider class as is. As a convenience there is a RealmProvider exposed to handle setting up the proper storeKey, this is probably what you want to use.

Connect

Again, react-redux is quite extensible so we can re-use a lot of the libraries core code. However, in order to work with Realm the connect() API needed to be changed.

connect realmConnect
-- mapPropsToQueries(realm, ownProps)
mapStateToProps(state, ownProps) mapQueriesToProps(queries, ownProps)
mapDispatchToProps(dispatch, ownProps) mapRealmDispatchToProps(dispatch, ownProps)
mergeProps(stateProps, dispatchProps, ownProps) mergeProps(queryProps, dispatchProps, ownProps)

API Docs

Coming soon!

Getting Started

Installation

A real npm package is coming soon, but to start using right away: npm install https://github.com/lolatravel/realm-react-redux/tarball/v0.0.5

Running the examples

There's only a basic example right now, more coming soon.

git clone [email protected]:lolatravel/realm-react-redux.git
cd realm-react-redux
npm install
cd examples/basic_todo_example
npm install
# if you don't have react-native-cli installed globally
npm install -g react-native-cli
react-native run-ios

Basic Example

// models.js
export class ToDo {
    static schema = {
        name: 'ToDo',
        primaryKey: 'id',
        properties: {
            id: { type: 'string' },
            name: { type: 'string' },
            completed: { type: 'bool', default: false }
        }
    };
}
// actions.js
export const CREATE_TODO = 'CREATE_TODO';
export const TOGGLE_TODO = 'TOGGLE_TODO';

export function createTodo(name) {
    return {
        type: CREATE_TODO,
        name
    };
}

export function toggleTodo(id) {
    return {
        type: TOGGLE_TODO,
        id
    };
}
// writers.js
import uuid from 'uuid/v4';
import { CREATE_TODO, TOGGLE_TODO } from './actions';

export function todoWriter(realm, action) {
    switch (action.type) {
        case CREATE_TODO:
            const { name } = action;
            realm.create('ToDo', {
                id: uuid(),
                name
            });
            break;

        case TOGGLE_TODO:
            const { id } = action;
            const todos = realm.objects('ToDo').filtered(`id = "${id}"`);
            if (todos.length === 1) {
                const todo = todos[0];
                todo.completed = !todo.completed;
            }
            break;

        default:
            break;
    }
}
// configureStore.js
import { createRealmStore, combineWriters } from 'realm-react-redux';
import { todoWriter } from './writers';
import { ToDo } from './models';

export function configureRealmStore() {
    // This will create a Realm instance to use in the store, using the options
    // passed in the second argument. To pass an existing Realm instance instead
    // you can use createRealmStore(writer, { realm: yourRealmInstance })
    return createRealmStore(combineWriters([todoWriter]), { schema: [ToDo] });
}
// ToDoListContainer.js
import { bindActionCreators } from 'redux';
import { realmConnect } from 'realm-react-redux';
import { createTodo, toggleTodo } from './actions';
import ToDoList from './ToDoList';

function mapPropsToQueries(realm) {
    return [realm.objects('ToDo')];
}

function mapQueriesToProps([todos]) {
    return {
        // Normally you would use a selector here to create simplified versions
        // of the model containing only what's needed by the UI for rendering.
        todos: todos.map(t => { return { id: t.id, name: t.name, completed: t.completed }; })
    };
}

function mapRealmDispatchToProps(dispatch) {
    return bindActionCreators({
        createTodo,
        toggleTodo
    }, dispatch);
}
// The ToDoList component will now receive todos, createTodo, and toggleTodo as props
export default realmConnect(mapPropsToQueries, mapQueriesToProps, mapRealmDispatchToProps)(ToDoList);

LICENSE

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].