13 January 2020

React hooks useState example 2

  • Create Components folder under src folder
  • Add ReactStateHookEXP2.js file and add below Code
import React, { useState } from "react";

function ReactStateHookEXP2() {
const [count, setCount] = useState(0);

const buttonClickHandler = e => {
setCount(count + 1);
};

return (
<>
<div>React State Hook EXP2</div>
<div>{count}</div>
<div>
<button onClick={buttonClickHandler}>Click</button>
</div>
</>
);
}
export default ReactStateHookEXP2;
  • Open index.js file and update code as below
import React from "react";
import ReactDOM from "react-dom";
import ReactStateHookEXP2 from "./components/ReactStateHookEXP2";

function App() {
return (
<div className="App">
<ReactStateHookEXP2 />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output:

No comments:

Post a Comment