Image will appear shortly

Handling data with input element in React

30 november

What is a React ?


A JavaScript library for building user interfaces

REACT is a Declaritive Liabrary so it takes care of DOM logic and makes it painless to create interactive UI's.



How to handle data with input element ?


we use OnChange event to get the value user is typing. Update the value using useState


onChange


In React, onChange is used to handle user input in real-time. If you want to build a form in React, you need to use this event to track the value of input elements. Now whenever you type something into the input box, React will trigger the function that we passed into the onChange prop.



useState


useState is used to declare variables inside a function. The value of useState() consist of an array with two values
[intial value, reference after update]

//state in event handler

var[like, setLike] = useState(0);

//like = intial , setLike = store the value after the update

function likeUpdate(){
var newLike = like+1;
setLike(newLike)
}



event.target


It return the element that triggers the event

//event.target

<input onChange= {someEvent}> </input>

//input is the element that triggered onChange event





Thankyou for reading !!

Go Back Next