Guest User

Untitled

a guest
Jan 4th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import { actionCreators } from './actions';
  4. import Input from './Input';
  5. import List from './List';
  6.  
  7. class App extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {};
  11. this.onAddTodo = this.onAddTodo.bind(this);
  12. this.onRemoveTodo = this.onRemoveTodo.bind(this);
  13. }
  14.  
  15. componentWillMount() {
  16. const { store } = this.props;
  17. const { todos } = store.getState();
  18. this.setState({ todos });
  19.  
  20. this.unsubscribe = store.subscribe(() => {
  21. const { todos } = store.getState();
  22. this.setState({ todos });
  23. });
  24. }
  25.  
  26. componentWillUnMount() {
  27. this.unsubscribe();
  28. }
  29.  
  30. onAddTodo(text) {
  31. const { store } = this.props;
  32. store.dispatch(actionCreators.addTodo(text));
  33. }
  34.  
  35. onRemoveTodo(index) {
  36. const { store } = this.props;
  37. store.dispatch(actionCreators.removeTodo(index));
  38. }
  39.  
  40. render() {
  41. const { todos } = this.state;
  42.  
  43. return (
  44. <div>
  45. <h1>Todo List</h1>
  46. <Input
  47. placeholder={'Add a todo'}
  48. handleSubmit={this.onAddTodo}
  49. />
  50. <List
  51. list={todos}
  52. onClickItem={this.onRemoveTodo}
  53. />
  54. </div>
  55. )
  56. }
  57. }
  58.  
  59. const mapStateToProps = (state) => ({
  60. todos: state.todos,
  61. })
  62.  
  63. export default connect(mapStateToProps)(App);
Add Comment
Please, Sign In to add comment