Lesson 2 - Overview of How State Works in React
State in React
How State Works
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0); // Initialize state with initial value 0
return (
<div>
<p>Count: {count}</p>
<button className="btn btn-primary mx-2"
onClick={()=>setCount(count+1)}>Increment</button>
<button className="btn btn-danger mx-2"
onClick={()=>setCount(count-1)}>Decrement</button>
</div>
);
}
export default Counter;Last updated