Skip to content

Instantly share code, notes, and snippets.

@balvinder294
Created February 13, 2025 07:13
Show Gist options
  • Save balvinder294/8ec5b6263538e5f031b1cf08463b6444 to your computer and use it in GitHub Desktop.
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/
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