Created
February 13, 2025 07:13
-
-
Save balvinder294/8ec5b6263538e5f031b1cf08463b6444 to your computer and use it in GitHub Desktop.
Example of using state as published in the article https://tekraze.com/understanding-states-and-props-in-reactjs-component/
This file contains hidden or 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'; | |
function MyComponent() { | |
const [count, setCount] = useState(0); | |
const incrementCount = () => { | |
setCount(prevCount => prevCount + 1); // Use functional update for correct state updates | |
}; | |
return ( | |
<div> | |
<p>Count: {count}</p> | |
<button onClick={incrementCount}>Increment</button> | |
</div> | |
); | |
} | |
export default MyComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment