Last active
August 24, 2016 01:45
-
-
Save keyserfaty/5cbeddb7b15cd2da56e95d38ef30c8ca to your computer and use it in GitHub Desktop.
Todos is React and Redux UI
This file contains hidden or 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 React from 'react'; | |
| import ui from 'redux-ui'; | |
| // TodosMainContainer.js | |
| const mapStateToProps = state => ({ | |
| todos: state.todos.todos | |
| }); | |
| const mapDispatchToProps = state => ({ | |
| addTodo: (data) => dispatch(addTodo(data)) | |
| }); | |
| export default connect(mapStateToProps, mapDispatchToProps)(TodosMain); | |
| // TodosMain.js | |
| const TodosMain = props => { | |
| const { | |
| todos, | |
| addTodo | |
| } = props; | |
| return ( | |
| <div className="container"> | |
| <AddTodo | |
| todos={todos} | |
| addTodo={addTodo} | |
| /> | |
| <TodosList | |
| todos={todos} | |
| /> | |
| </div> | |
| ); | |
| }; | |
| export default TodosMain; | |
| // AddTodo.js | |
| const initialState = { | |
| todo: '' | |
| }; | |
| const AddTodo => props => { | |
| const { | |
| todos, | |
| addTodo | |
| } = props; | |
| const handleOnClick (e) => { | |
| const todo = e.target.value; | |
| addTodo(todo); | |
| }; | |
| const handleOnChange (e) => { | |
| const todo = e.target.value; | |
| updateUI({ todo }); | |
| }; | |
| return ( | |
| <div> | |
| <input | |
| type="text" | |
| placeholder="Add your todo here" | |
| name="todo" | |
| value={ui.todo} | |
| onChange={(e) => handleOnChange(e)} | |
| /> | |
| <button | |
| onClick={(e) => handleOnClick(e)} | |
| disable={!ui.todo} | |
| >Add<button> | |
| </div> | |
| ); | |
| }; | |
| AddTodo.propTypes = { | |
| ui: React.PropTypes.object.isRequired, | |
| updateUI: React.PropTypes.func.isRequired, | |
| todos: React.PropTypes.object.isRequired, | |
| addTodo: React.PropTypes.func.isRequired | |
| }; | |
| export default ui({ state: initialState })(AddTodo); | |
| // TodoList.js | |
| const TodoList = props => { | |
| const { | |
| todos | |
| } = props; | |
| return ( | |
| <ul> | |
| { todos.map(todo => ( | |
| <li>{todo}</li> | |
| ))} | |
| </ul> | |
| ) | |
| }; | |
| TodoList.propTypes = { | |
| todos: React.PropTypes.object.isRequired | |
| }; | |
| export default TodoList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment