Form in React JS#35

 APP.jsx


import React, { useState } from 'react';



const App = () => {

const [name1,updateName] = useState("");
const [fullName,setfullName] = useState();

const inputEvent = (event) =>{
updateName(event.target.value);
// console.log(event.target.value);
};

const onsubmit = () => {

setfullName(name1);

}


return(
<>
<div>
<h1> Hello {fullName} </h1>
<input type="text" placeholder="Enter your Name"
onChange={inputEvent}
value={name1}
/>
<button onClick={onsubmit}> Click Me</button>
</div>

</>
);


}

export default App;

Update Value using form tag:

import React, { useState } from 'react';



const App = () => {

const [name1,updateName] = useState("");
const [fullName,setfullName] = useState();

const inputEvent = (event) =>{
updateName(event.target.value);
// console.log(event.target.value);
};

const onsubmit = (event) => {
event.preventDefault();

setfullName(name1);

}


return(
<>
<form onSubmit={onsubmit}>
<div>
<h1> Hello {fullName} </h1>
<input type="text" placeholder="Enter your Name"
onChange={inputEvent}
value={name1}
/>
<br/>
<button type="submit"> Click Me</button>
</div>
</form>

</>
);


}

export default App;


vv

Comments

Popular posts from this blog

Form Part 2

Event mini project by Thapa#33

De-mystifying 'this' keyword of Javascript