zhongqf / meteoredux

A small package that make Redux connected with Meteor.
MIT License
16 stars 2 forks source link

meteoredux

A small package that make Redux connected with Meteor, in Redux way. Inspired by Meteor React Packages.

Briefly, when the data you subscribed from Meteor changed :

Usage

1. Define your Meteor queries in Reducer
import { bindReactiveData } from 'meteoredux'
const initialState = {};

function todos(state = initialState, action) {

  switch (action.type) {
  case ADD_TODO:
    Todos.insert({
      completed: false,
      title: action.title
    });
    //We have not changed the state here, so we return the original state.
    //Or even you can update your Meteor data inside a component.
    return state;
  default:
    return state;
  }
}

function reactiveData(){
  return {
    todos: Todos.find({}).fetch()
  }
}
export default bindReactiveData(todos, reactiveData);
2. Connect your Redux store to Meteor

index.js

import { createStore, combineReducers } from 'redux';
import { connectToMeteor } from 'meteoredux'

Meteor.subscribe('todos');

let combinedReducers = combineReducers(reducers);
let store = createStore(combinedReducers);

connectToMeteor(store);

Do not connect to Meteor twice.

3. Done!

Retrieve your Meteor data in Redux's state just like something else.

let todos = store.getState().todos;

Or when used multi reducers

let todos = store.getState().todosReducer.todos;

Example

https://github.com/zhongqf/meteor-react-redux-example https://github.com/zhongqf/meteor-vue-redux-example