Skip to content

Commit 9e6f99b

Browse files
committed
react-redux-hooks initial commit
1 parent 81e2f9e commit 9e6f99b

File tree

2 files changed

+19
-38
lines changed

2 files changed

+19
-38
lines changed

src/App.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
import React from 'react';
22
import './App.css';
3-
import {connect} from 'react-redux'
43
import TodosReduxContainer from './components/TodosReduxContainer';
5-
// import TodosContainer from './components/TodosContainer';
4+
import { useSelector } from 'react-redux';
65

76
function App(props) {
7+
const todos = useSelector(state => state.todos);
8+
const inputValue = useSelector(state => state.inputValue);
89
return (
910
<div className="App">
1011
<div className="container">
1112
<div className="header">
1213
<h1>Todo List</h1>
1314
</div>
14-
<TodosReduxContainer {...props}/>
15+
<TodosReduxContainer {...props} todos={todos} inputValue={inputValue}/>
1516
</div>
1617
</div>
1718
);
1819
}
1920

20-
const mapStateToProps = (state) => {
21-
return {
22-
todos: state.todos,
23-
inputValue: state.inputValue
24-
}
25-
}
26-
27-
export default connect(mapStateToProps)(App);
21+
export default App;
Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
import {connect} from 'react-redux'
21
import React, { useState } from "react";
32
import { Spinner } from 'reactstrap';
4-
import { deleteToDo, updateTodo, addTodo, handleTextChange, removeText } from './actions/todoActions';
3+
import { useDispatch } from 'react-redux';
4+
import { deleteToDo, updateTodo, addTodo } from './actions/todoActions';
55

66

7-
const handleDelete = (props, id) => {
8-
props.deleteToDo(id)
7+
const handleDelete = (props, id, dispatch) => {
8+
dispatch(deleteToDo(id))
99
}
1010

11-
const handleUpdate = (e, id, props) => {
12-
props.updateTodo(id, e.target.checked)
11+
const handleUpdate = (e, id, props, dispatch) => {
12+
dispatch(updateTodo(id, e.target.checked))
1313
}
1414

15-
const createTodo = (e, inputValue, setinputValue, props) => {
15+
const createTodo = (e, inputValue, setinputValue, props, dispatch) => {
1616
if(e.key === 'Enter'){
17-
props.addTodo(inputValue)
17+
dispatch(addTodo(inputValue))
1818
setinputValue("")
1919
}
2020
}
2121

22-
// const handleChange = (e, props) => {
23-
// props.handleTextChange(e.target.value)
24-
// }
25-
2622
const TodosReduxContainer = (props) => {
2723
const [inputValue, setinputValue] = useState("");
24+
const dispatch = useDispatch();
2825
if(props && props.todos.length === 0){
2926
return(
3027
<div style={{textAlign: 'center'}}>
@@ -40,10 +37,10 @@ const TodosReduxContainer = (props) => {
4037
type="text"
4138
placeholder="Add a task"
4239
maxLength="50"
43-
onKeyPress={(e) => createTodo(e, inputValue, setinputValue, props)}
40+
onKeyPress={(e) => createTodo(e, inputValue, setinputValue, props, dispatch)}
4441
value={inputValue || ''}
4542
// onChange={(e) => handleChange(e, props)}
46-
onChange={e => setinputValue(e.target.value)}
43+
onChange={e => setinputValue(e.target.value)}
4744
/>
4845
</div>
4946
<div className="listWrapper">
@@ -53,11 +50,11 @@ const TodosReduxContainer = (props) => {
5350
<li className="task" todo={todo} key={todo.id}>
5451
<input className="taskCheckbox" type="checkbox"
5552
checked={todo.done}
56-
onChange={(e) => handleUpdate(e, todo.id, props)}
53+
onChange={(e) => handleUpdate(e, todo.id, props, dispatch)}
5754
/>
5855
<label className="taskLabel">{todo.title}</label>
5956
<span className="deleteTaskBtn"
60-
onClick={(e) => handleDelete(props, todo.id)}>
57+
onClick={(e) => handleDelete(props, todo.id, dispatch)}>
6158
x
6259
</span>
6360
</li>
@@ -70,14 +67,4 @@ const TodosReduxContainer = (props) => {
7067
}
7168
}
7269

73-
const mapDispatchToProps = (dispatch) => {
74-
return {
75-
deleteToDo: (id) => {dispatch(deleteToDo(id))},
76-
addTodo: (data) => {dispatch(addTodo(data))},
77-
updateTodo: (id, checked) => {dispatch(updateTodo(id, checked))},
78-
removeText: () => {dispatch(removeText())},
79-
handleTextChange: (data) => {dispatch(handleTextChange(data))}
80-
}
81-
}
82-
83-
export default connect(null, mapDispatchToProps)(TodosReduxContainer)
70+
export default TodosReduxContainer

0 commit comments

Comments
 (0)