Skip to content

Instantly share code, notes, and snippets.

@codebucks27
Last active April 22, 2021 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebucks27/5271735a3afde89edb2b2f146760a923 to your computer and use it in GitHub Desktop.
Save codebucks27/5271735a3afde89edb2b2f146760a923 to your computer and use it in GitHub Desktop.
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