Really liked your tutorial Tim! <3 I thought I try all of that with functional components and this is what I got (feel free to correct me - I'm kinda rookie in webdev). Seems working though!
Minor changes I made:
used functional components instead of class components. Therefore used React.useState() for storing variables in the state.
input element for adding new todo is a controlled component and handled also in the state. Found this approach more "react-ish".
for deleting a todo I also used filter() method - seems more handy for me.
some naming changes
App.js
// App.jsimportReact,{useState,Fragment}from"react";importListfrom"./List";constApp=()=>{const[userInput,setUserInput]=useState("");const[list,setList]=useState(["walk the dog","buy the milk","learn some code"]);// userinput is controlled by the App componentconsthandleChange=e=>{setUserInput(e.target.value);};constaddItem=e=>{if(userInput!==""){setList([...list,userInput]);setUserInput("");}};constremoveItem=item=>{constupdatedList=list.filter(listItem=>listItem!==item);setList(updatedList);};return(<Fragment><Listlist={list}removeItem={removeItem}/><hr/><form><inputplaceholder="Something that needs to be done..."value={userInput}onChange={handleChange}/><buttontype="button"onClick={addItem}>{'Add Item'}</button></form></Fragment>);}exportdefaultApp;
Hi there,
Really liked your tutorial Tim! <3 I thought I try all of that with functional components and this is what I got (feel free to correct me - I'm kinda rookie in webdev). Seems working though!
Minor changes I made:
used functional components instead of class components. Therefore used React.useState() for storing variables in the state.
input element for adding new todo is a controlled component and handled also in the state. Found this approach more "react-ish".
for deleting a todo I also used
filter()
method - seems more handy for me.some naming changes
App.js
List component:
default
create-react-app
index.js...