13 January 2020

React hooks useState example 1

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

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

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

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

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

Output:

No comments:

Post a Comment