Skip to content

Instantly share code, notes, and snippets.

@kaiobrito
Created July 20, 2018 20:42
Show Gist options
  • Save kaiobrito/06dfa5773f851cc96f160c952b1a2a02 to your computer and use it in GitHub Desktop.
Save kaiobrito/06dfa5773f851cc96f160c952b1a2a02 to your computer and use it in GitHub Desktop.
Todo List + Redux + React Component
export const actionTypes = {
REFRESH_LIST: "TODOS/LIST_REFRESH"
};
export const actionsCreators = {
updateList: todos => ({
type: actionTypes.REFRESH_LIST,
payload: todos
})
};
const INITIAL_STATE = fromJS({
items: {}
});
export default (state = INITIAL_STATE, { type, payload }) => {
switch (type) {
case actionTypes.REFRESH_LIST:
return state.set("items", fromJS(payload));
default:
return state;
}
};
class TodoList extends React.Component {
componentDidMount() {
this.listener = database.ref("todos").on("value", snapshot => {
this.props.updateList(snapshot.val() || {});
});
}
componentWillUnmount() {
this.listener.off();
}
render() {
return (
<ul>
{this.props.todos.map(todo => (
<li key={todo.get("id")}>{todo.get("text")}</li>
))}
</ul>
);
}
}
const mapActions = {
updateList: actionsCreators.updateList
};
export default connect(
null,
mapActions
)(TodoList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment