Skip to content

Instantly share code, notes, and snippets.

@myogeshchavan97
Last active April 4, 2021 17:04
Show Gist options
  • Save myogeshchavan97/0531ae630a32aeb38d605b97a580d8fb to your computer and use it in GitHub Desktop.
Save myogeshchavan97/0531ae630a32aeb38d605b97a580d8fb to your computer and use it in GitHub Desktop.
Multiple useState calls in single component
import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";
const App = () => {
const [counter, setCounter] = useState(0);
const [username, setUsername] = useState("");
useEffect(() => {
console.log("useEffect executed");
});
return (
<div>
<div>
<p>Counter value is: {counter} </p>
<button onClick={() => setCounter(counter + 1)}>Increment</button>
<br />
<br />
Name{" "}
<input
type="text"
name="username"
value={username}
placeholder="Enter your name"
autoComplete="off"
onChange={event => setUsername(event.target.value)}
/>
<br />
{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