You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importReactfrom'react'import{useState}from'react'interfaceProps{}interfaceItem{id: number;value: number;}interfacePerson{firstName: string;lastName: string;}constHookCounter : React.FC<Props>=()=>{//S1 create functional component//S3 : method capable of setting that state varconstinitialCount=0const[count,setCount]=useState(0)// this hook accepts initial val, and returns current val, and const[name,setName]=useState({firstName : '',lastName: ''})// Ch 4: object in useStateconst[items,setItems]=useState([])// Ch 5: array in useStateconstincrementFive=()=>{setCount(prevCount=>prevCount+5)}constaddItem=()=>{setItems([ ...items,//copy prev array for merge{id: items.length,value: Math.floor(Math.random()*10)+1}//newitem])//setItems}//addItemreturn(<div>
Ch 3: Using previous state: <em> setCount(prevCount =>prevCount+1)</em>
Count : {count}<br/><buttononClick={()=>setCount(prevCount=>prevCount+1)}> More + </button><br/>{/* For incrementfive no ()=> */}<buttononClick={incrementFive}> More +5 </button><br/><buttononClick={()=>setCount(prevCount=>prevCount-1)}> Less - </button><br/><buttononClick={()=>setCount(initialCount)}> Reset </button><hr/>
Ch 4: Using useState with object: <em>useState(json-object)</em><br/>
Manual merge with spread operator: <em>onChange= e = setName( ...name, firstName : e.target.value )</em><form>{/* TODO check why should I mention both firstname and lastname in setname() */}<inputtype="text"value={name.firstName}onChange={e=>setName({ ...name,firstName : e.target.value})}/><br/><inputtype="text"value={name.lastName}onChange={e=>setName({ ...name,lastName : e.target.value})}/><br/></form>
Your first name is: <em>{name.firstName}</em><br/>
Your last name is: <em>{name.lastName}</em><br/>
state variable name : {JSON.stringify(name)}<hr/>
Ch 5: Using useState with arrays: <em>useState(json-object)</em><br/><buttononClick={addItem}>Add a number</button><ul>{items.map(item=>(<likey={item.id}>{item.value}</li>))}//items
</ul></div>);//return};exportdefaultHookCounter;
The text was updated successfully, but these errors were encountered:
Thanks for the tutorial, I'm going through the vidoes and trying to learn Hooks with typescript.
How do I get rid of the below error? Would be great to know.
https://stackoverflow.com/questions/63855787/react-hook-tsx-and-untyped-array
HookCounter.tsx
The text was updated successfully, but these errors were encountered: