-
-
Save codebucks27/5271735a3afde89edb2b2f146760a923 to your computer and use it in GitHub Desktop.
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
import React, { useState } from "react"; | |
import { connect } from "react-redux"; | |
import { addTodos } from "../redux/reducer"; | |
const mapStateToProps = (state) => { | |
return { | |
todos: state, | |
}; | |
}; | |
const mapDispatchToProps = (dispatch) => { | |
return { | |
addTodo: (obj) => dispatch(addTodos(obj)), | |
}; | |
}; | |
const Todos = (props) => { | |
console.log("props",props); | |
const [todo, setTodo] = useState(""); | |
const add = () => { | |
if (todo === "") { | |
alert("Input is Empty"); | |
} else { | |
props.addTodo({ | |
id: Math.floor(Math.random() * 1000), | |
item: todo, | |
completed: false, | |
}); | |
setTodo(""); | |
} | |
}; | |
const handleChange = (e) => { | |
setTodo(e.target.value); | |
}; | |
return ( | |
<div className="addTodos"> | |
<input | |
type="text" | |
onChange={(e) => handleChange(e)} | |
className="todo-input" | |
value={todo} | |
/> | |
<button className="add-btn" onClick={() => add()}> | |
Add | |
</button> | |
<br /> | |
<ul> | |
{props.todos.length > 0 && | |
props.todos.map((item) => { | |
return <li key={item.id}>{item.item}</li>; | |
})} | |
</ul> | |
</div> | |
); | |
}; | |
//we can use connect method to connect this component with redux store | |
export default connect(mapStateToProps, mapDispatchToProps)(Todos); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment