Last active
April 4, 2021 17:07
-
-
Save myogeshchavan97/d3781b1109e69c72edd67b6eb5d9f8c6 to your computer and use it in GitHub Desktop.
Using object in useState
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
import ReactDOM from "react-dom"; | |
const App = () => { | |
const [state, setState] = useState({ | |
counter: 0, | |
username: "" | |
}); | |
const handleOnClick = () => { | |
setState({ | |
counter: state.counter + 1 | |
}); | |
}; | |
const handleOnChange = event => { | |
setState({ | |
username: event.target.value | |
}); | |
}; | |
return ( | |
<div> | |
<div> | |
<p>Counter value is: {state.counter} </p> | |
<button onClick={handleOnClick}>Increment</button> | |
<br /> | |
<br /> | |
Name{" "} | |
<input | |
type="text" | |
name="username" | |
value={state.username} | |
placeholder="Enter your name" | |
autoComplete="off" | |
onChange={handleOnChange} | |
/> | |
<br /> | |
{state.username} | |
</div> | |
</div> | |
); | |
}; | |
ReactDOM.render(<App />, document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment