Skip to content

Answer #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
14 changes: 11 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
//imports useState hook
import { useState } from 'react'

//imports components
import Header from "./components/Header";
import List from "./components/List"
import AddTodo from "./components/AddTodo"


//creating your App function
function App() {

//sets useState
const [todos, setTodos] = useState([
{
id: 1,
Expand All @@ -20,12 +26,13 @@ function App() {
},
]);

//creates addTodo function
const addTodo = (todo) => {
const id = Math.ceil(Math.random()*100000)
const newTodo = {id, ...todo}
setTodos([...todos, newTodo])
const newTodo = {id, ...todo} //creates new todo
setTodos([...todos, newTodo]) //calls setTodos to add newTodo along with pervious todos
}

//what App.js returns
return (
<div className="container">
<Header title="Todo List" />
Expand All @@ -35,4 +42,5 @@ function App() {
);
}

// exporting App.js
export default App;
4 changes: 4 additions & 0 deletions src/components/AddTodo.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { useState } from 'react'

//creation of AddToDo component, passing in onAdd
const AddTodo = ({ onAdd }) => {
const [item, setItem] = useState('')

//eventhandler
const onSubmit = (e) => {
e.preventDefault()
onAdd({item})

setItem('')
}

//the form to add todo
return (
<form onSubmit={onSubmit}>
<label>Add Todo:</label>
Expand All @@ -24,4 +27,5 @@ const AddTodo = ({ onAdd }) => {
)
}

//exporting of AddTodo
export default AddTodo
3 changes: 3 additions & 0 deletions src/components/Header.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";

// header component that takes in props
const Header = (props) => {
return (
<div>
Expand All @@ -8,9 +9,11 @@ const Header = (props) => {
);
};

//styling for Header
const headerStyle = {
fontSize: "40px",
textDecoration: "underline",
};

//exporting of Header
export default Header;
3 changes: 3 additions & 0 deletions src/components/List.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//importing Todo component
import Todo from './Todo'

//creation of List component, passing in todos
const List = ({ todos }) => {
return (
<div>
Expand All @@ -10,5 +12,6 @@ const List = ({ todos }) => {
)
}

//exporting List component
export default List

4 changes: 2 additions & 2 deletions src/components/Todo.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

//creating of Todo component
const Todo = ({ todo }) => {
return (
<div>
<h2>{todo.item}</h2>
</div>
)
}

//exporting of Todo
export default Todo