Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ const App = () => {
const [todos, setTodos] = useState(todosData);

// get the newTodo from NewTodo.js here inside this function
const handleAddTodo = () => {};
const handleAddTodo = (newTodo) => {
// This is for deep cloning the state beacuse of avoiding re-rendering issues
const todosClone = JSON.parse(JSON.stringify(todos));
todosClone.push(newTodo);
setTodos(todosClone);
};

return (
<div>
<NewTodo />
<Todos />
<NewTodo handleAddTodo={handleAddTodo} />
<Todos todos={todos} />
</div>
);
};

export default App;
28 changes: 19 additions & 9 deletions src/components/NewTodo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@ import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { v4 as uuidv4 } from 'uuid';

const NewTodo = (props) => {
const [todo, setTodo] = useState();
const NewTodo = ({ handleAddTodo }) => {
const [todo, setTodo] = useState({ title: '', desc: '' });

// for handling todo state changes
const handleChange = (e) => {};
const handleChange = (e) => {
setTodo((prevTodo) => {
return { ...prevTodo, [e.target.name]: e.target.value };
});
};

// submit the form and send newTodo in App.js
const handleSubmit = (e) => {
e.preventDefault();
const newTodo = {
id: uuidv4()
id: uuidv4(),
...todo
};

// This function will add todo at the app state
if (todo.title && todo.desc) {
handleAddTodo(newTodo);
setTodo({
title: '',
desc: ''
});
}

// for reset todo state
setTodo({
title: '',
desc: ''
});
};

return (
Expand Down Expand Up @@ -60,7 +70,7 @@ const NewTodo = (props) => {
};

NewTodo.propTypes = {
onHandleAddTodo: PropTypes.func
handleAddTodo: PropTypes.func
};

export default NewTodo;