Skip to content

Instantly share code, notes, and snippets.

@gabrielhpugliese
Last active February 29, 2016 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielhpugliese/a405e93b5dd3aaa1fe85 to your computer and use it in GitHub Desktop.
Save gabrielhpugliese/a405e93b5dd3aaa1fe85 to your computer and use it in GitHub Desktop.
import { connect } from 'react-redux';
import { todos } from './reactive-redux';
const mapStateToProps = ({ todos }) => ({ todos });
@connect(mapStateToProps)
export default class TodoMain extends Component {
render() {
const { todos } = this.props;
return (
<div className={style.container}>
<TodoList tasks={todos} />
</div>
);
}
};
import { createStore, combineReducers } from 'redux';
const updateTodos = (todos) => {
return {
type: 'UPDATE_TODOS',
todos
}
};
export const todos = (state = [], action) => {
switch (action.type) {
case 'UPDATE_TODOS':
return action.todos;
default:
return state
}
}
const reducers = combineReducers({ todos });
const store = createStore(reducers);
store.autorun = (f) => {
Tracker.autorun(() => {
f(store.dispatch);
});
};
// Below would work like a store refresher. Whenever the collection changes we change the store too.
store.autorun(dispatch => {
const allTodos = Todos.find().fetch();
if (allTodos.length) {
dispatch(updateTodos(allTodos));
}
});
store.subscribe(() => console.log(store.getState()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment