Last active
February 29, 2016 02:40
-
-
Save gabrielhpugliese/a405e93b5dd3aaa1fe85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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