Created
July 20, 2018 20:42
-
-
Save kaiobrito/06dfa5773f851cc96f160c952b1a2a02 to your computer and use it in GitHub Desktop.
Todo List + Redux + React Component
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
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; | |
} | |
}; |
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
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